Skip to content

Commit 54dd8e4

Browse files
authored
Merge pull request #14 from rhkiswani/develop
Develop
2 parents c1c4dff + 06483cd commit 54dd8e4

25 files changed

Lines changed: 263 additions & 56 deletions

README.md

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,31 @@ JavaFF: Java Facade/Factories
66

77
We all know the Golden Object Oriented rule **Don't talk to strangers**
88

9-
We all know that the API/frameworks defects or magic or limitations is not showing at the begging of the development.
9+
We all know that you will never find the API/frameworks defects or magic or limitations the begging of the development.
1010

11-
So imagine that you using an API in all your projects and after spending months or years that API you got a production issue becuase of it ,
11+
So imagine you are using an API in all your projects and after spending months or years that API you got a production issue because of it even if it so famous and mature,
1212
changing the that API would be soo hard and costy !!!
13-
13+
14+
Examples for famous bugs in very famous frameworks
15+
---------------------------------------------------
16+
Memory Leak
17+
- [(POI) OOM caused by Memory Leak in FileBackedDataSource ](https://bz.apache.org/bugzilla/show_bug.cgi?id=60140)
18+
- [(MySql JDBC connector) Memory leak in ResultSet](https://bugs.mysql.com/bug.php?id=5022)
19+
- [(Gson) Memory Leak in web application](https://github.com/google/gson/issues/402)
20+
21+
Performance
22+
- [(LOG4J) log4j performance tuning in production config](https://bugzilla.redhat.com/show_bug.cgi?id=778690)
23+
- [(Guava) performance problem in LinkedHashMultimap](https://github.com/google/guava/issues/1013)
24+
25+
Deadlocks
26+
- [(Oracle JDK) deadlock in SSLSocketImpl between between write and close](http://bugs.java.com/view_bug.do?bug_id=8013809)
27+
28+
So, we should always protect our project and noy use a framwork or API directly and this is the main idea here
1429

1530
Main Features
1631
--------------
1732
- This project offers a standard/clear API for the most used API's in the Java Applications like : Exceptions, Locale, Beans, Formatter's, Json Handlers, Loggers, ReflectionHelpers ...etc
18-
- **you can control the implementation's through the class path without changing line of code**
33+
- **You can control the implementation's through the class path without changing line of code**
1934
- Smart Exception handling mechanism
2035
- Default Implementations
2136
- Many Utilises

src/main/java/io/github/rhkiswani/javaff/format/DateFormatter.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@
1515
*/
1616
package io.github.rhkiswani.javaff.format;
1717

18+
import io.github.rhkiswani.javaff.exceptions.SmartException;
1819
import io.github.rhkiswani.javaff.format.exception.FormatException;
20+
import io.github.rhkiswani.javaff.lang.utils.StringUtils;
1921
import io.github.rhkiswani.javaff.log.Log;
2022
import io.github.rhkiswani.javaff.log.LogFactory;
2123

@@ -38,14 +40,21 @@ class DateFormatter extends DefaultFormatter<Date, String> {
3840
protected String formatVal(Date date, Object... params) {
3941
if (params.length > 0 && params[0] != null){
4042
try {
43+
if (StringUtils.isEmpty(params[0] + "")){
44+
throw new FormatException(SmartException.TYPE_ERROR, "Date format", date);
45+
}
4146
SimpleDateFormat format = new SimpleDateFormat(params[0].toString());
4247
return format.format(date);
4348
}catch (Throwable t){
4449
LOGGER.error("Failed to format {0} due to {1} ", date, t.getMessage());
45-
throw new FormatException(t);
50+
throw new FormatException(SmartException.TYPE_ERROR, "Date format", date);
4651
}
4752
} else {
4853
return MessageFormat.format("{0}", date);
4954
}
5055
}
56+
57+
public String format(Date date, String pattern){
58+
return format(date , new Object[]{pattern});
59+
}
5160
}

src/main/java/io/github/rhkiswani/javaff/format/DefaultFormatter.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,12 @@
2323
* @since 0.0.1
2424
* @see io.github.rhkiswani.javaff.format.Formatter
2525
*/
26-
public abstract class DefaultFormatter<IN, OUT> implements Formatter<IN, OUT> {
26+
public abstract class DefaultFormatter<IN, OUT> extends Formatter<IN, OUT> {
2727

2828
protected abstract OUT formatVal(IN in, Object[] params);
2929

3030
@Override
31-
public OUT format(IN in, Object[] params) throws FormatException {
31+
protected OUT format(IN in, Object[] params) throws FormatException {
3232
if (in == null){
3333
return null;
3434
}
@@ -37,11 +37,14 @@ public OUT format(IN in, Object[] params) throws FormatException {
3737
ArraysUtils.replace(params, null, "");
3838
return formatVal(in, params);
3939
} catch (Throwable t ){
40+
if (t instanceof FormatException) {
41+
throw t;
42+
}
4043
throw new FormatException(t);
4144
}
4245

4346
}
44-
return formatVal(in, null);
47+
return formatVal(in, new Object[]{});
4548
}
4649

4750
}

src/main/java/io/github/rhkiswani/javaff/format/FormatUtil.java

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,6 @@
1515
*/
1616
package io.github.rhkiswani.javaff.format;
1717

18-
import io.github.rhkiswani.javaff.exceptions.SmartException;
19-
import io.github.rhkiswani.javaff.lang.exceptions.IllegalParamException;
20-
2118
/**
2219
* @author Mohamed Kiswani
2320
* @since 0.0.1
@@ -35,7 +32,7 @@ public static String formatString(String str, Object... params){
3532

3633
public static <T>T format(Object obj, Object... params){
3734
if (obj == null){
38-
throw new IllegalParamException(SmartException.NULL_VAL, "Object");
35+
return null;
3936
}
4037
return (T) FormatFactory.getFormatter(obj.getClass()).format(String.valueOf(obj), params);
4138
}

src/main/java/io/github/rhkiswani/javaff/format/Formatter.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@
2222
* @since 0.0.1
2323
*
2424
*/
25-
public interface Formatter<IN, OUT> {
25+
public abstract class Formatter<IN, OUT> {
2626

27-
OUT format(IN in, Object[] params) throws FormatException;
27+
protected abstract OUT format(IN in, Object[] params) throws FormatException;
2828

2929
}

src/main/java/io/github/rhkiswani/javaff/format/NumberFormatter.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,7 @@ protected String formatVal(Number number, Object... params) {
3030
return MessageFormat.format("{0}", number);
3131
}
3232

33+
public String format(Number num){
34+
return format(num, new Object[]{});
35+
}
3336
}

src/main/java/io/github/rhkiswani/javaff/json/GsonHandler.java

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
import com.google.gson.Gson;
1919
import com.google.gson.GsonBuilder;
20+
import io.github.rhkiswani.javaff.json.exceptions.JsonException;
2021

2122
import java.lang.reflect.Modifier;
2223

@@ -31,12 +32,20 @@ class GsonHandler implements JsonHandler {
3132

3233
@Override
3334
public <T> T fromJson(String json, Class clazz) {
34-
return (T) gson.fromJson(json, clazz);
35+
try {
36+
return (T) gson.fromJson(json, clazz);
37+
} catch (Throwable t){
38+
throw new JsonException(t);
39+
}
3540
}
3641

3742
@Override
3843
public String toJson(Object object) {
39-
return gson.toJson(object);
44+
try{
45+
return gson.toJson(object);
46+
} catch (Throwable t){
47+
throw new JsonException(t);
48+
}
4049
}
4150

4251
@Override

src/main/java/io/github/rhkiswani/javaff/lang/HashCodeHelper.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,16 @@ protected Integer doAction(Object obj) {
3232
return -1;
3333
}
3434
HashCodeBuilder hashCodeBuilder = new HashCodeBuilder();
35+
boolean atLestOneValueInserted = false;
3536
for (Field field : getFieldsByAnnotation(obj, HashcodeField.class)) {
36-
hashCodeBuilder.append(reflectionHelper.getFieldValue(obj, field.getName()));
37+
Object val = reflectionHelper.getFieldValue(obj, field.getName());
38+
if (val != null){
39+
atLestOneValueInserted = true;
40+
hashCodeBuilder.append(val);
41+
}
42+
3743
}
38-
return hashCodeBuilder.toHashCode();
44+
return atLestOneValueInserted ? hashCodeBuilder.toHashCode() : -1;
3945
}
4046

4147
public int toHashCode(Object obj) {

src/main/java/io/github/rhkiswani/javaff/lang/utils/StringUtils.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
package io.github.rhkiswani.javaff.lang.utils;
1717

1818
import io.github.rhkiswani.javaff.lang.ToStringHelper;
19-
import io.github.rhkiswani.javaff.security.encode.EncodeFactory;
19+
import io.github.rhkiswani.javaff.security.escape.EscapeersFactory;
2020

2121
/**
2222
* @author Mohamed Kiswani
@@ -29,8 +29,8 @@ public static String toString(Object obj) {
2929
return new ToStringHelper().toString(obj);
3030
}
3131

32-
public static String encode(String input) {
33-
return (String) EncodeFactory.getEncoder(String.class).encode(input);
32+
public static String escape(String input) {
33+
return (String) EscapeersFactory.getEscapeer(String.class).escape(input);
3434
}
3535

3636
public static boolean isEmpty(String input){

src/main/java/io/github/rhkiswani/javaff/security/encode/DefaultEncodeHandler.java renamed to src/main/java/io/github/rhkiswani/javaff/security/escape/DefaultEscapeHandler.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,22 +13,22 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16-
package io.github.rhkiswani.javaff.security.encode;
16+
package io.github.rhkiswani.javaff.security.escape;
1717

1818
import io.github.rhkiswani.javaff.lang.utils.ArraysUtils;
19-
import io.github.rhkiswani.javaff.security.encode.exception.EncodeException;
19+
import io.github.rhkiswani.javaff.security.escape.exception.EncodeException;
2020

2121
/**
2222
* @author Mohamed Kiswani
2323
* @since 0.0.1
24-
* @see io.github.rhkiswani.javaff.security.encode.EncodeHandler
24+
* @see EscapeHandler
2525
*/
26-
public abstract class DefaultEncodeHandler<IN, OUT> implements EncodeHandler<IN, OUT> {
26+
public abstract class DefaultEscapeHandler<IN, OUT> implements EscapeHandler<IN, OUT> {
2727

2828
protected abstract OUT encodeVal(IN in, Object... params);
2929

3030
@Override
31-
public OUT encode(IN in, Object... params) throws EncodeException {
31+
public OUT escape(IN in, Object... params) throws EncodeException {
3232
if (in == null){
3333
return null;
3434
}

0 commit comments

Comments
 (0)