Skip to content

Commit cbaa3ea

Browse files
authored
Feature/fixs documentation added (#15)
* Documentation updated, fixes
1 parent 2f1fca3 commit cbaa3ea

13 files changed

Lines changed: 395 additions & 101 deletions

File tree

README.md

Lines changed: 148 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,11 @@ 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 you will never find the API/frameworks defects or magic or limitations the begging of the development.
9+
We all know that you will never find the API/frameworks defects or magic or limitations the at begging of the development.
1010

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,
12-
changing the that API would be soo hard and costy !!!
11+
Imagine yourself using API in all your projects for years. Everybody else is using it as well, it is famous and mature. Suddenly you have a huge production issue. Changing that API is too hard and costly at this stage!
12+
13+
What now?
1314

1415
Examples for famous bugs in very famous frameworks
1516
---------------------------------------------------
@@ -25,16 +26,153 @@ Performance
2526
Deadlocks
2627
- [(Oracle JDK) deadlock in SSLSocketImpl between between write and close](http://bugs.java.com/view_bug.do?bug_id=8013809)
2728

28-
So, we should always protect our project and noy use a framwork or API directly and this is the main idea here
29+
So, we should always protect our project and noy use a framework or API directly and this is the main idea here
2930

3031
Main Features
3132
--------------
32-
- 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
33-
- **You can control the implementation's through the class path without changing line of code**
34-
- Smart Exception handling mechanism
35-
- Default Implementations
36-
- Many Utilises
37-
33+
- 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.
34+
35+
- **You can control the implementations through the class path without changing line of code**
36+
The below example shows how the implementation will be changed without changing the code:
37+
Now I have the below dependencies in my pom.xml
38+
```xml
39+
<dependency>
40+
<groupId>org.slf4j</groupId>
41+
<artifactId>slf4j-api</artifactId>
42+
<version>${slf4.version}</version>
43+
<scope>provided</scope>
44+
</dependency>
45+
<dependency>
46+
<groupId>org.slf4j</groupId>
47+
<artifactId>slf4j-jdk14</artifactId>
48+
<version>${slf4.version}</version>
49+
<scope>provided</scope>
50+
</dependency>
51+
```
52+
So when I call
53+
```javascript
54+
LogFactory.getLogger(LogFactory.class).info("LOCALIZED_MSG", "Kiswani");
55+
output:
56+
Nov 22, 2016 6:54:07 PM io.github.rhkiswani.javaff.log.Slf4jLog info
57+
INFO: this is localized msg from messages_en.properties thanks for Mr Kiswani
58+
```
59+
60+
When I remove the dependencies from the pom.xml and run the same code I will get
61+
62+
```javascript
63+
LogFactory.getLogger(LogFactory.class).info("LOCALIZED_MSG", "Kiswani");
64+
output:
65+
Nov 22, 2016 7:00:15 PM io.github.rhkiswani.javaff.log.DefaultLog info
66+
INFO: this is localized msg from messages_en.properties thanks for Mr Kiswani
67+
```
68+
69+
- Transparent localization for logs, strings, exceptions
70+
71+
- Centralized and Configurable Exception Handling by the class type below a full example
72+
```java
73+
74+
package io.github.rhkiswani.javaff;
75+
76+
import io.github.rhkiswani.javaff.exceptions.ExceptionHandler;
77+
import io.github.rhkiswani.javaff.exceptions.ExceptionHandlersFactory;
78+
import io.github.rhkiswani.javaff.exceptions.ExceptionUtil;
79+
80+
public class TestMain {
81+
82+
public static void main(String[] args) {
83+
//Any class is instance of ConsoleException will be handled here
84+
ExceptionHandlersFactory.instance().add(ConsoleException.class, new ExceptionHandler() {
85+
@Override
86+
public void handle(Throwable t) {
87+
System.out.println("ConsoleException handler");
88+
}
89+
});
90+
91+
//Any class is instance of MailException will be handled here
92+
ExceptionHandlersFactory.instance().add(MailException.class, new ExceptionHandler() {
93+
@Override
94+
public void handle(Throwable t) {
95+
System.out.println("MailException handler");
96+
}
97+
});
98+
99+
100+
ExceptionUtil.handle(new ConsoleException());
101+
ExceptionUtil.handle(new SubConsoleException());
102+
ExceptionUtil.handle(new MailException());
103+
ExceptionUtil.handle(new SubMailException());
104+
105+
//Null pointer is not related to the perilous class's it will be handled by default handler for Throwable.class
106+
//which is painting the stack trace by default
107+
ExceptionUtil.handle(new NullPointerException());
108+
109+
//We decided to override the default implantation for Throwable.class
110+
ExceptionHandlersFactory.instance().overrideImp(Throwable.class, new ExceptionHandler() {
111+
@Override
112+
public void handle(Throwable t) {
113+
System.out.println("Overridden handler");
114+
}
115+
});
116+
117+
ExceptionUtil.handle(new NullPointerException());
118+
}
119+
120+
private static class ConsoleException extends RuntimeException{
121+
122+
}
123+
124+
private static class SubConsoleException extends ConsoleException{
125+
126+
}
127+
128+
private static class MailException extends RuntimeException{
129+
130+
}
131+
132+
private static class SubMailException extends MailException {
133+
134+
}
135+
136+
}
137+
138+
```
139+
140+
- Logging with localization out of the box
141+
142+
```java
143+
LogFactory.getLogger(LogFactory.class).info("LOCALIZED_MSG", "Kiswani");
144+
output : INFO: this is localized msg from messages_en.properties thanks for Mr Kiswani
145+
```
146+
147+
```java
148+
LogFactory.getLogger(LogFactory.class).info("normal msg num {0} date {1}", Integer.MAX_VALUE, new Date());
149+
INFO: normal msg num 2,147,483,647 date 11/22/16 6:06 PM
150+
```
151+
152+
- Many Utilities, below is just examples
153+
- Formatter's
154+
```java
155+
System.out.println(FormatUtil.format("Mr {0} {1}", "Mohamed", "Kiswani"));
156+
System.out.println(FormatUtil.format(new Date()));
157+
System.out.println(FormatUtil.format(new Date(), "yyyy-MM-dd"));
158+
System.out.println(FormatUtil.format(Integer.MAX_VALUE));
159+
160+
Mr Mohamed Kiswani
161+
11/22/16 6:16 PM
162+
2016-11-22
163+
2,147,483,647
164+
```
165+
166+
- Json Handlers
167+
```java
168+
System.out.println(JsonHandlerFactory.getJsonHandler(TestMain.class).toJson(new Employee(1000)));
169+
output : {"id":0,"name":null,"empId":1000}
170+
```
171+
```java
172+
System.out.println(JsonHandlerFactory.getJsonHandler(TestMain.class).fromJson("{\"id\":100,\"name\":null,\"empId\":1000}", Employee.class));
173+
output: Employee[id=100]
174+
```
175+
38176
Prerequisites
39177
-------------
40178
Requires JDK 1.7 or higher.

pom.xml

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,14 @@
110110
<dependency>
111111
<groupId>org.slf4j</groupId>
112112
<artifactId>slf4j-api</artifactId>
113-
<version>1.7.21</version>
113+
<version>${slf4.version}</version>
114+
<scope>provided</scope>
115+
</dependency>
116+
117+
<dependency>
118+
<groupId>org.slf4j</groupId>
119+
<artifactId>slf4j-jdk14</artifactId>
120+
<version>${slf4.version}</version>
114121
<scope>provided</scope>
115122
</dependency>
116123

src/main/java/io/github/rhkiswani/javaff/factory/AbstractFactory.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import java.util.Collection;
2222
import java.util.LinkedHashMap;
2323
import java.util.Map;
24+
import java.util.Set;
2425

2526
/**
2627
* @author Mohamed Kiswani
@@ -57,7 +58,10 @@ protected IMP_TYPE create(Class targetClass){
5758
if (userDefaultImpl != null){
5859
return userDefaultImpl;
5960
}
60-
for (Class aClass : map.keySet()) {
61+
Set<Class> classSet = map.keySet();
62+
Class[] keys = classSet.toArray(new Class[classSet.size()]);
63+
for (int i = keys.length - 1 ; i >=0 ; i--){
64+
Class aClass = keys[i];
6165
if (aClass.isAssignableFrom(targetClass) || targetClass.isAnnotationPresent(aClass)){
6266
return map.get(aClass);
6367
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,6 @@ public static <T>T format(Object obj, Object... params){
3434
if (obj == null){
3535
return null;
3636
}
37-
return (T) FormatFactory.getFormatter(obj.getClass()).format(String.valueOf(obj), params);
37+
return (T) FormatFactory.getFormatter(obj.getClass()).format(obj, params);
3838
}
3939
}

src/main/java/io/github/rhkiswani/javaff/locale/LocaleWorkersFactory.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@ public class LocaleWorkersFactory extends AbstractFactory<LocaleWorker>{
2727
private static LocaleWorkersFactory instance = new LocaleWorkersFactory();
2828

2929
private LocaleWorkersFactory(){
30-
add(Throwable.class, new LocaleWorkerBuilder().path("exceptions/").build());
3130
add(Object.class, new LocaleWorkerBuilder().path("app/").build());
31+
add(Throwable.class, new LocaleWorkerBuilder().path("exceptions/").build());
3232
}
3333

3434
public static LocaleWorkersFactory instance(){

src/main/java/io/github/rhkiswani/javaff/log/LogFactory.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,6 @@ protected Log getDefault(Class targetClazz) {
4242
}
4343

4444
public static Log getLogger(Class aClass) {
45-
return instance.create(aClass);
45+
return new LogWrapper(instance.create(aClass));
4646
}
4747
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/*
2+
* Copyright 2016 Mohamed Kiswani.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package io.github.rhkiswani.javaff.log;
18+
19+
import io.github.rhkiswani.javaff.locale.LocaleUtil;
20+
21+
/**
22+
* @author Mohamed Kiswani
23+
* @since 0.0.17
24+
*
25+
*/
26+
public class LogWrapper implements Log{
27+
28+
private final Log log;
29+
30+
public LogWrapper(Log log) {
31+
this.log = log;
32+
}
33+
34+
@Override
35+
public void debug(String message, Object... params) {
36+
log.debug(LocaleUtil.getString(message, params));
37+
}
38+
39+
@Override
40+
public void info(String message, Object... params) {
41+
log.info(LocaleUtil.getString(message, params));
42+
}
43+
44+
@Override
45+
public void warn(String message, Object... params) {
46+
log.warn(LocaleUtil.getString(message, params));
47+
}
48+
49+
@Override
50+
public void error(String message, Object... params) {
51+
log.error(LocaleUtil.getString(message, params));
52+
}
53+
54+
@Override
55+
public void error(String message, Exception e, Object... params) {
56+
log.error(LocaleUtil.getString(message, params), e);
57+
}
58+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
LOCALIZED_MSG=this is localized msg from messages_en.properties thanks for Mr {0}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package io.github.rhkiswani.javaff;
2+
3+
public class TestMain {
4+
5+
public static void main(String[] args) {
6+
7+
}
8+
9+
10+
11+
}

src/test/java/io/github/rhkiswani/javaff/beans/withEqualsAnnotation/Employee.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,15 @@ public class Employee extends Person<Employee>{
44

55
private int empId;
66

7+
public Employee() {
8+
9+
}
10+
11+
public Employee(int empId) {
12+
super();
13+
this.empId = empId;
14+
}
15+
716
public int getEmpId() {
817
return empId;
918
}

0 commit comments

Comments
 (0)