Skip to content

Commit

Permalink
Develop (#43)
Browse files Browse the repository at this point in the history
* #26 #25

* code style fixed (#28)

* Feature/http client tests code quilty (#30)

* code style fixed

* #29 #26

* code-quilty

* code-quilty (#32)

* code style fixs

* release 0.0.23 updated

* release 0.0.23 updated

* #23

* #25

* #25 (#35)

* improve coverage

* improve coverage

* improve coverage

* improve coverage

* Feature/imporve coverage (#37)

* #25

* improve coverage

* improve coverage

* improve coverage

* improve coverage

* #39 (#40)

* Feature/validation coverage (#42)

* #39

* #41
  • Loading branch information
rhkiswani authored Dec 4, 2016
1 parent cd38e4d commit 0994b10
Show file tree
Hide file tree
Showing 38 changed files with 678 additions and 101 deletions.
7 changes: 7 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
<java.version>1.7</java.version>

<gson.version>2.8.0</gson.version>
<javax.validation.api.version>1.0.0.GA</javax.validation.api.version>
<jackson.version>2.8.5</jackson.version>
<commons.lang.version>3.5</commons.lang.version>
<slf4.version>1.7.21</slf4.version>
Expand Down Expand Up @@ -81,6 +82,12 @@
<version>${commons.lang.version}</version>
</dependency>

<dependency>
<groupId>javax.validation</groupId>
<artifactId>validation-api</artifactId>
<version>${javax.validation.api.version}</version>
</dependency>

<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,6 @@ public class ApiDetectorUtil {
private static final Boolean isApacheHttpClientAvailable = ApiDetectorFactory.getDetector().isAvailable(APACHE_HTTPCLIENT_API_METADATA);


private ApiDetectorUtil(){}

public static Boolean isJPAAvailable() {
return isJPAAvailable;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@
*/
public class ExceptionUtil {

private ExceptionUtil(){}

public static void handle(Throwable t){
if (t == null){
throw new IllegalParamException(SmartException.NULL_VAL, "Exception");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ public class SmartException extends RuntimeException{
public static final String NEGATIVE_VAL = "NEGATIVE_VAL";
public static final String HTTP_ERROR = "HTTP_ERROR";
public static final String NO_IMPLEMENTATION_FOUND = "NO_IMPLEMENTATION_FOUND";
public static final String NO_IMPLEMENTATION_FOUND_WITH_NO_LINK = "NO_IMPLEMENTATION_FOUND_WITH_NO_LINK";

private Object[] errorMsgParams = null;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
import io.github.rhkiswani.javaff.exceptions.SmartException;
import io.github.rhkiswani.javaff.factory.exceptions.NoImplementationFoundException;
import io.github.rhkiswani.javaff.lang.exceptions.IllegalParamException;
import io.github.rhkiswani.javaff.lang.utils.ObjectUtils;
import io.github.rhkiswani.javaff.lang.utils.StringUtils;

import java.util.Collection;
import java.util.LinkedHashMap;
Expand All @@ -39,24 +41,37 @@ public void add(Class targetClass, T t){
if (map.get(targetClass) != null){
throw new IllegalParamException(SmartException.ALREADY_EXIST, targetClass, "please use overrideImp method instated");
}
if (targetClass.isPrimitive()){
targetClass = ObjectUtils.primitiveToWrapper(targetClass);
}
map.put(targetClass, t);
}

public void overrideImp(Class targetClass, T t){
if (targetClass.isPrimitive()){
targetClass = ObjectUtils.primitiveToWrapper(targetClass);
}
map.put(targetClass, t);
}

public T remove(Class targetClass){
if (targetClass.isPrimitive()){
targetClass = ObjectUtils.primitiveToWrapper(targetClass);
}
return map.remove(targetClass);
}

protected T create(Class targetClass){
if (targetClass == null){
protected T create(Class clazz){
if (clazz == null){
throw new IllegalParamException(SmartException.NULL_VAL, "Target Class");
}
if (userDefaultImpl != null){
return userDefaultImpl;
}
Class targetClass = clazz;
if (targetClass.isPrimitive()){
targetClass = ObjectUtils.primitiveToWrapper(targetClass);
}
Set<Class> classSet = map.keySet();
Class[] keys = classSet.toArray(new Class[classSet.size()]);
for (int i = keys.length - 1 ; i >=0 ; i--){
Expand All @@ -77,10 +92,14 @@ public void setDefault(T userDefaultImpl) {
}

protected T getDefault(Class targetClazz) {
throw new NoImplementationFoundException(SmartException.NO_IMPLEMENTATION_FOUND, this.getClass().getSimpleName(), this.getClass().getSimpleName(), getDefaultImplementationUrl());
if (StringUtils.isNotEmpty(getDefaultImplementationUrl())){
throw new NoImplementationFoundException(SmartException.NO_IMPLEMENTATION_FOUND, targetClazz, this.getClass().getSimpleName(), getDefaultImplementationUrl());
} else {
throw new NoImplementationFoundException(SmartException.NO_IMPLEMENTATION_FOUND_WITH_NO_LINK, targetClazz, this.getClass().getSimpleName());
}
}

protected String getDefaultImplementationUrl(){
return "https://mvnrepository.com";
return "";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,8 @@ protected O format(I i, Object[] params) throws FormatException {
return null;
}
if (!ArraysUtils.isEmpty(params)){
try {
ArraysUtils.replace(params, null, "");
return formatVal(i, params);
} catch (FormatException e){
throw e;
} catch (Throwable t ){
throw new FormatException(t.getMessage());
}

ArraysUtils.replace(params, null, "");
return formatVal(i, params);
}
return formatVal(i, new Object[]{});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ protected Formatter getDefault(Class targetClazz){
return new StringFormatter();
}

public static Formatter getFormatter(Class aClass) {
public static <I, O> Formatter<I, O> getFormatter(Class aClass) {
return instance.create(aClass);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@
*/
public class FormatUtil {

private FormatUtil(){}

public static String formatString(String str, Object... params){
return format(str, params);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ public Map<String, String> head(String url, Map<String, String> params, Map<Stri
HttpHead method = new HttpHead(url);
CloseableHttpClient client = prepareRequest(method, params);
setHeaders(method, headers);
HttpResponse response= client.execute(method);
HttpResponse response = client.execute(method);
Header[] s = response.getAllHeaders();
Map<String, String> returnedHeaders = new HashMap<>();
for (Header header : s) {
Expand All @@ -162,10 +162,6 @@ public Map<String, String> options(String url, Map<String, String> params, Map<S
return head(url, params, headers);
}

@Override
public String patch(String url, Map<String, String> params, Map<String, String> headers) throws HttpClientException {
return doRequest(new HttpPatch(url), params, headers);
}

@Override
public String patchJson(String url, String json, Map<String, String> headers) throws HttpClientException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,5 @@ public interface HttpClient {

Map<String, String> options(String url, Map<String, String> params, Map<String, String> headers) throws HttpClientException;

String patch(String url, Map<String, String> params, Map<String, String> headers) throws HttpClientException;

String patchJson(String url, String json, Map<String, String> headers) throws HttpClientException;
}
Original file line number Diff line number Diff line change
Expand Up @@ -71,11 +71,6 @@ public Map<String, String> options(String url, Map<String, String> params, Map<S
return httpClient.options(url, params, headers);
}

@Override
public String patch(String url, Map<String, String> params, Map<String, String> headers) throws HttpClientException {
return httpClient.patch(url, params, headers);
}

@Override
public String patchJson(String url, String json, Map<String, String> headers) throws HttpClientException {
return httpClient.patchJson(url, json, headers);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,7 @@ public <T> T fromJson(String json, Class clazz) {

@Override
public String toJson(Object object) {
try{
return gson.toJson(object);
} catch (Throwable t){
throw new JsonException(t.getMessage());
}
return gson.toJson(object);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,9 @@
*/
package io.github.rhkiswani.javaff.json;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.github.rhkiswani.javaff.json.exceptions.JsonException;

import java.io.IOException;

/**
* @author Mohamed Kiswani
* @since 0.0.1
Expand Down
24 changes: 13 additions & 11 deletions src/main/java/io/github/rhkiswani/javaff/lang/ToStringHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ public class ToStringHelper extends AbstractObjectHelper<Object, String>{

@Override
protected String doAction(Object obj) {
if (obj == null){
return null;
}
StringBuilder builder = new StringBuilder();
builder.append(obj.getClass().getSimpleName());
builder.append("[");
Expand All @@ -45,15 +48,11 @@ protected String doAction(Object obj) {
}

private String gosnToString(Object obj) {
try {
String json = JsonHandlerFactory.getJsonHandler(GsonBean.class).toJson(obj);
json = json.replace("{", "[");
json = json.replace("}", "]");
json = json.replace("\"", "");
return json;
}catch (Exception e){
return "[ can't print " + obj.getClass() + " value]";
}
String json = JsonHandlerFactory.getJsonHandler(GsonBean.class).toJson(obj);
json = json.replace("{", "[");
json = json.replace("}", "]");
json = json.replace("\"", "");
return json;
}

private String normalToString(Object obj) {
Expand All @@ -67,10 +66,13 @@ private String normalToString(Object obj) {
}
for (int i = 0; i < fields.size() ; i++) {
Field f = fields.get(i);
Object fieldValue = reflectionHelper.getFieldValue(obj, f.getName());
if (fieldValue == null){
continue;
}
builder.append(f.getName());
builder.append("=");
Object fieldValue = reflectionHelper.getFieldValue(obj, f.getName());
if (fieldValue != null && (fieldValue.getClass().isArray() || Collection.class.isInstance(fieldValue))){
if ((fieldValue.getClass().isArray() || Collection.class.isInstance(fieldValue))){
builder.append(gosnToString(fieldValue));
} else {
builder.append(FormatUtil.format("{0}", fieldValue));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,10 @@
*/
package io.github.rhkiswani.javaff.lang.utils;

import io.github.rhkiswani.javaff.exceptions.SmartException;
import io.github.rhkiswani.javaff.lang.EqualsHelper;
import io.github.rhkiswani.javaff.lang.HashCodeHelper;
import io.github.rhkiswani.javaff.lang.exceptions.IllegalParamException;

/**
* @author Mohamed Kiswani
Expand All @@ -32,4 +34,31 @@ public static boolean isEqual(Object obj1, Object obj2) {
public static int toHashCode(Object obj) {
return new HashCodeHelper().toHashCode(obj);
}

public static Class primitiveToWrapper(Class targetClass) {
if (targetClass == null){
throw new IllegalParamException(SmartException.NULL_VAL, "Target Class");
}
if (!targetClass.isPrimitive()){
throw new IllegalParamException("{0} is not primitive", targetClass.getSimpleName());
}
if (targetClass.equals(byte.class)){
return Byte.class;
} else if (targetClass.equals(char.class)) {
return Character.class;
} else if (targetClass.equals(short.class)) {
return Short.class;
} else if (targetClass.equals(int.class)) {
return Integer.class;
} else if (targetClass.equals(long.class)) {
return Long.class;
} else if (targetClass.equals(float.class)) {
return Float.class;
} else if (targetClass.equals(double.class)) {
return Double.class;
} else if (targetClass.equals(boolean.class)) {
return Boolean.class;
}
return targetClass;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,16 @@ public static String toString(Object obj) {
public static boolean isEmpty(String input){
return input == null || input.isEmpty();
}

public static boolean isNotEmpty(String input){
return !isEmpty(input);
}

public static boolean isNull(String input){
return isEmpty(input) || "NULL".equalsIgnoreCase(input);
}

public static boolean isNotNull(String input){
return !isNull(input);
}
}
15 changes: 1 addition & 14 deletions src/main/java/io/github/rhkiswani/javaff/locale/LocaleUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
import io.github.rhkiswani.javaff.exceptions.SmartException;
import io.github.rhkiswani.javaff.format.FormatUtil;
import io.github.rhkiswani.javaff.lang.utils.StringUtils;
import io.github.rhkiswani.javaff.reflection.ReflectionUtil;

import java.util.HashMap;
import java.util.Map;
Expand All @@ -32,10 +31,6 @@ public class LocaleUtil {

private static Map<String, String> DEFAULT_MSGS = null;

private LocaleUtil(){

}

static {
DEFAULT_MSGS = new HashMap<>();
DEFAULT_MSGS.put(SmartException.EXCEEDS_LIMIT, "{0} MaxSize is {1}");
Expand All @@ -47,15 +42,7 @@ private LocaleUtil(){
DEFAULT_MSGS.put(SmartException.NEGATIVE_VAL, "{0} should be greater or equal 0");
DEFAULT_MSGS.put(SmartException.HTTP_ERROR, "failed to connect to {0}");
DEFAULT_MSGS.put(SmartException.NO_IMPLEMENTATION_FOUND, "No implementation found for {0} you need to set implementation through {1}.instance().add or add {2} to your classpath");
}

@Deprecated
/**
* use public static String getString(String key, Class targetClass, Object[] params) instead
*/
public static String getString(String key, Object[] params){
Class targetClass = ReflectionUtil.getCallerClass(1);
return getString(key, targetClass, params);
DEFAULT_MSGS.put(SmartException.NO_IMPLEMENTATION_FOUND_WITH_NO_LINK, "No implementation found for {0} you need to set implementation through {1}.instance().add ");
}

public static String getString(String key, Class targetClass, Object[] params) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,6 @@ protected Log getDefault(Class targetClazz) {
}

public static Log getLogger(Class aClass) {
return new LogWrapper(instance.create(aClass));
return new LogWrapper(instance().create(aClass));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ public void error(String message, Object... params) {
log.error(LocaleUtil.getString(message, LogWrapper.class, params));
}

@Override
public void error(String message, Exception e, Object... params) {
log.error(LocaleUtil.getString(message, LogWrapper.class, params), e);
}
Expand Down
Loading

0 comments on commit 0994b10

Please sign in to comment.