Skip to content

Commit 8de19e9

Browse files
committed
Added wrapper utilities
1 parent 6924956 commit 8de19e9

File tree

5 files changed

+118
-8
lines changed

5 files changed

+118
-8
lines changed

docgen/release-notes.txt

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1-
0.7.4.7 (2021-01-01)
1+
0.7.4.8 (2021-01-XX)
2+
--------------------
3+
+ Added wrappers utilities
4+
5+
0.7.4.7 (2021-01-01)
26
--------------------
37
+ Added ValidatorNumber for ValidatorCatalog A.P.I.
48

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,22 @@
11
package org.fugerit.java.core.db.daogen;
22

3-
public class BasicWrapper<T> extends BasicHelper {
3+
import org.fugerit.java.core.lang.helpers.Wrapper;
4+
import org.fugerit.java.core.lang.helpers.WrapperHelper;
5+
6+
public class BasicWrapper<T> extends BasicHelper implements Wrapper<T> {
47
/**
58
*
69
*/
710
private static final long serialVersionUID = -2800606962171545339L;
811

912
private T wrapped;
1013

14+
@Override
1115
public T unwrapModel() {
1216
return wrapped;
1317
}
1418

19+
@Override
1520
public void wrapModel(T wrapped) {
1621
this.wrapped = wrapped;
1722
}
@@ -25,13 +30,9 @@ public String toString() {
2530
return this.getClass().getSimpleName()+"[wraps:"+unwrapModel().toString()+"]";
2631
}
2732

28-
@SuppressWarnings("unchecked")
33+
@Override
2934
public T unwrap() {
30-
T res = this.unwrapModel();
31-
while ( res instanceof BasicWrapper ) {
32-
res = ((BasicWrapper<T>)res).unwrapModel();
33-
}
34-
return res;
35+
return WrapperHelper.unwrap( this.wrapped );
3536
}
3637

3738
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package org.fugerit.java.core.lang.helpers;
2+
3+
/**
4+
*
5+
* Interface for wrapping / unwrapping objects
6+
*
7+
* @author Matteo a.k.a. Fugerit
8+
* @since 0.7.4.8
9+
*
10+
* @param <T> the type of the object to wrap
11+
*/
12+
public interface Wrapper<T> {
13+
14+
/**
15+
* Unwrap the underlying object
16+
*
17+
* @return the wrapped object
18+
*/
19+
public T unwrapModel();
20+
21+
/**
22+
*
23+
* Wraps the give objects
24+
*
25+
* @param wrapped objects to wrap
26+
*/
27+
public void wrapModel(T wrapped);
28+
29+
/**
30+
* Recursively unwrap the wrapped object until it is not instance of Wrapper anymore.
31+
*
32+
* @return the underlying object
33+
*/
34+
public T unwrap();
35+
36+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package org.fugerit.java.core.lang.helpers;
2+
3+
/**
4+
*
5+
* Collections of utility method for handling Wrappers.
6+
*
7+
* @author Matteo a.k.a. Fugerit
8+
* @since 0.7.4.8
9+
* @see Wrapper
10+
*
11+
*/
12+
public class WrapperHelper {
13+
14+
/**
15+
* Recursively unwrap one object as long as it's implementing Wrapper interface
16+
*
17+
* @param <T> the type of the object to be unwrapped
18+
* @param model the object to be unwrapped
19+
* @return the unwrapped model
20+
*/
21+
@SuppressWarnings("unchecked")
22+
public static <T> T unwrap( T model ){
23+
T res = model;
24+
if ( res != null ) {
25+
while ( res instanceof Wrapper ) {
26+
res = ((Wrapper<T>)res).unwrapModel();
27+
}
28+
}
29+
return res;
30+
}
31+
32+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package org.fugerit.java.core.validator;
2+
3+
import org.fugerit.java.core.lang.helpers.Wrapper;
4+
5+
public class ValidatorWrapper extends BasicValidator implements Wrapper<BasicValidator> {
6+
7+
/**
8+
*
9+
*/
10+
private static final long serialVersionUID = 2745412297380349295L;
11+
12+
private BasicValidator wrapped;
13+
14+
public ValidatorWrapper( BasicValidator wrapped ) {
15+
this.wrapped = wrapped;
16+
}
17+
18+
@Override
19+
public BasicValidator unwrapModel() {
20+
return this.wrapped;
21+
}
22+
23+
@Override
24+
public void wrapModel(BasicValidator wrapped) {
25+
this.wrapped = wrapped;
26+
}
27+
28+
@Override
29+
public BasicValidator unwrap() {
30+
BasicValidator res = this.unwrapModel();
31+
while ( res instanceof ValidatorWrapper ) {
32+
res = ((ValidatorWrapper)res).unwrapModel();
33+
}
34+
return res;
35+
}
36+
37+
}

0 commit comments

Comments
 (0)