4 package net
.kezvh
.collections
.views
;
6 import java
.util
.AbstractMap
;
10 import net
.kezvh
.collections
.MapEntryImpl
;
11 import net
.kezvh
.functional
.Operations
;
12 import net
.kezvh
.functional
.lambda
.L1
;
19 public class MapAsView
<S
, D
> extends AbstractMap
<S
, D
> {
21 private final Set
<S
> sourceSet
;
22 private final L1
<D
, S
> mapOp
;
23 private final L1
<S
, D
> inverseMapOp
;
25 private final L1
<Entry
<S
, D
>, S
> entryOp
= new L1
<Entry
<S
, D
>, S
>() {
27 * @see net.kezvh.functional.lambda.L1#op(java.lang.Object)
28 * @param param COMMENT
32 public Map
.Entry
<S
, D
> op(final S param
) {
33 return new MapEntryImpl
<S
, D
>(param
, MapAsView
.this.mapOp
.op(param
));
36 private final L1
<S
, Entry
<S
, D
>> inverseEntryOp
= new L1
<S
, Entry
<S
, D
>>() {
38 * @see net.kezvh.functional.lambda.L1#op(java.lang.Object)
39 * @param param COMMENT
43 public S
op(final Map
.Entry
<S
, D
> param
) {
44 return param
.getKey();
47 private final Set
<Entry
<S
, D
>> entrySet
;
50 * @param sourceSet COMMENT
52 * @param inverseOp COMMENT
54 public MapAsView(final Set
<S
> sourceSet
, final L1
<D
, S
> op
, final L1
<S
, D
> inverseOp
) {
56 this.sourceSet
= sourceSet
;
58 this.inverseMapOp
= inverseOp
;
59 this.entrySet
= new SetView
<S
, Map
.Entry
<S
, D
>>(this.sourceSet
, this.entryOp
, this.inverseEntryOp
);
63 * @param sourceSet COMMENT
66 @SuppressWarnings("unchecked")
67 public MapAsView(final Set
<S
> sourceSet
, final L1
<D
, S
> op
) {
68 this(sourceSet
, op
, (L1
<S
, D
>) Operations
.UNSUPPORTED
);
72 * @see java.util.AbstractMap#entrySet()
76 public Set
<Map
.Entry
<S
, D
>> entrySet() {
80 @SuppressWarnings("unchecked")
82 public D
get(final Object key
) {
83 return this.mapOp
.op((S
) key
);
86 @SuppressWarnings("unchecked")
88 public boolean containsValue(final Object value
) {
89 if (Operations
.UNSUPPORTED
.equals(this.inverseEntryOp
))
90 return super.containsValue(value
);
92 return this.sourceSet
.contains(this.inverseMapOp
.op((D
) value
));
96 public boolean containsKey(final Object key
) {
97 return this.sourceSet
.contains(key
);
101 public int hashCode() {
102 final int prime
= 31;
103 int result
= super.hashCode();
104 result
= prime
* result
+ ((this.inverseMapOp
== null) ?
0 : this.inverseMapOp
.hashCode());
105 result
= prime
* result
+ ((this.mapOp
== null) ?
0 : this.mapOp
.hashCode());
106 result
= prime
* result
+ ((this.sourceSet
== null) ?
0 : this.sourceSet
.hashCode());
111 public boolean equals(final Object obj
) {
114 if (!super.equals(obj
))
116 if (this.getClass() != obj
.getClass())
118 final MapAsView
<?
, ?
> other
= (MapAsView
<?
, ?
>) obj
;
119 if (this.inverseMapOp
== null) {
120 if (other
.inverseMapOp
!= null)
122 } else if (!this.inverseMapOp
.equals(other
.inverseMapOp
))
124 if (this.mapOp
== null) {
125 if (other
.mapOp
!= null)
127 } else if (!this.mapOp
.equals(other
.mapOp
))
129 if (this.sourceSet
== null) {
130 if (other
.sourceSet
!= null)
132 } else if (!this.sourceSet
.equals(other
.sourceSet
))