|
Last change
on this file was
3,
checked in by dsowen, 21 years ago
|
|
Copyright notice fixed.
Added containment accessor.
|
-
Property svn:eol-style set to
native
-
Property svn:keywords set to
Author Date Id Revision
|
|
File size:
905 bytes
|
| Line | |
|---|
| 1 | /* |
|---|
| 2 | * This code is copyright (c) 2005 David Owen. All rights not explicitly |
|---|
| 3 | * granted by the author are reserved. |
|---|
| 4 | * |
|---|
| 5 | * Created on Aug 2, 2005. |
|---|
| 6 | */ |
|---|
| 7 | package ws.fugue88.access; |
|---|
| 8 | |
|---|
| 9 | import java.util.Map; |
|---|
| 10 | |
|---|
| 11 | /** |
|---|
| 12 | * @author dsowen |
|---|
| 13 | */ |
|---|
| 14 | public class MapAccessor implements Accessor { |
|---|
| 15 | |
|---|
| 16 | public MapAccessor(final Map map, final Object key, final Class type) |
|---|
| 17 | { |
|---|
| 18 | map.getClass(); |
|---|
| 19 | type.getClass(); |
|---|
| 20 | |
|---|
| 21 | _map = map; |
|---|
| 22 | _key = key; |
|---|
| 23 | _type = type; |
|---|
| 24 | } |
|---|
| 25 | |
|---|
| 26 | public Class getType() |
|---|
| 27 | { |
|---|
| 28 | return _type; |
|---|
| 29 | } |
|---|
| 30 | |
|---|
| 31 | public void clearValue() |
|---|
| 32 | { |
|---|
| 33 | _map.remove(_key); |
|---|
| 34 | } |
|---|
| 35 | |
|---|
| 36 | public Object getValue() |
|---|
| 37 | { |
|---|
| 38 | return check(_map.get(_key)); |
|---|
| 39 | } |
|---|
| 40 | |
|---|
| 41 | public void setValue(final Object value) |
|---|
| 42 | { |
|---|
| 43 | _map.put(_key, check(value)); |
|---|
| 44 | } |
|---|
| 45 | |
|---|
| 46 | private Object check(final Object obj) |
|---|
| 47 | { |
|---|
| 48 | if(obj != null && !_type.isInstance(obj)) |
|---|
| 49 | throw new ClassCastException(); |
|---|
| 50 | return obj; |
|---|
| 51 | } |
|---|
| 52 | |
|---|
| 53 | private final Map _map; |
|---|
| 54 | private final Object _key; |
|---|
| 55 | private final Class _type; |
|---|
| 56 | } |
|---|
Note: See
TracBrowser
for help on using the repository browser.