With Lombok
01 import java.util.ArrayList;
02 import java.util.Collection;
03
04 import lombok.Delegate;
05
06 public class DelegationExample {
07 private interface SimpleCollection {
08 boolean add(String item);
09 boolean remove(Object item);
10 }
11
12 @Delegate(types=SimpleCollection.class)
13 private final Collection<String> collection = new ArrayList<String>();
14 }
15
16
17 class ExcludesDelegateExample {
18 long counter = 0L;
19
20 private interface Add {
21 boolean add(String x);
22 boolean addAll(Collection<? extends String> x);
23 }
24
25 @Delegate(excludes=Add.class)
26 private final Collection<String> collection = new ArrayList<String>();
27
28 public boolean add(String item) {
29 counter++;
30 return collection.add(item);
31 }
32
33 public boolean addAll(Collection<? extends String> col) {
34 counter += col.size();
35 return collection.addAll(col);
36 }
37 }
Vanilla Java
01 import java.util.ArrayList;
02 import java.util.Collection;
03 import lombok.Delegate;
04
05 public class DelegationExample {
06 private interface SimpleCollection {
07 boolean add(String item);
08 boolean remove(Object item);
09 }
10
11 private final Collection<String> collection = new ArrayList<String>();
12
13 @java.lang.SuppressWarnings("all")
14 public boolean add(final java.lang.String item) {
15 return this.collection.add(item);
16 }
17
18 @java.lang.SuppressWarnings("all")
19 public boolean remove(final java.lang.Object item) {
20 return this.collection.remove(item);
21 }
22 }
23
24 class ExcludesDelegateExample {
25 long counter = 0L;
26
27 private interface Add {
28 boolean add(String x);
29 boolean addAll(Collection<? extends String> x);
30 }
31
32 private final Collection<String> collection = new ArrayList<String>();
33
34 public boolean add(String item) {
35 counter++;
36 return collection.add(item);
37 }
38
39 public boolean addAll(Collection<? extends String> col) {
40 counter += col.size();
41 return collection.addAll(col);
42 }
43
44 @java.lang.SuppressWarnings("all")
45 public int size() {
46 return this.collection.size();
47 }
48
49 @java.lang.SuppressWarnings("all")
50 public boolean isEmpty() {
51 return this.collection.isEmpty();
52 }
53
54 @java.lang.SuppressWarnings("all")
55 public boolean contains(final java.lang.Object arg0) {
56 return this.collection.contains(arg0);
57 }
58
59 @java.lang.SuppressWarnings("all")
60 public java.util.Iterator<java.lang.String> iterator() {
61 return this.collection.iterator();
62 }
63
64 @java.lang.SuppressWarnings("all")
65 public java.lang.Object[] toArray() {
66 return this.collection.toArray();
67 }
68
69 @java.lang.SuppressWarnings("all")
70 public <T extends .java.lang.Object>T[] toArray(final T[] arg0) {
71 return this.collection.<T>toArray(arg0);
72 }
73
74 @java.lang.SuppressWarnings("all")
75 public boolean remove(final java.lang.Object arg0) {
76 return this.collection.remove(arg0);
77 }
78
79 @java.lang.SuppressWarnings("all")
80 public boolean containsAll(final java.util.Collection<?> arg0) {
81 return this.collection.containsAll(arg0);
82 }
83
84 @java.lang.SuppressWarnings("all")
85 public boolean removeAll(final java.util.Collection<?> arg0) {
86 return this.collection.removeAll(arg0);
87 }
88
89 @java.lang.SuppressWarnings("all")
90 public boolean retainAll(final java.util.Collection<?> arg0) {
91 return this.collection.retainAll(arg0);
92 }
93
94 @java.lang.SuppressWarnings("all")
95 public void clear() {
96 this.collection.clear();
97 }
98 }