|
1 | | -/* |
2 | | - * Copyright 2015 AndroidPlot.com |
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 com.androidplot.util; |
18 | | - |
19 | | -import java.util.HashMap; |
20 | | -import java.util.List; |
21 | | - |
22 | | -/** |
23 | | - * An implementation of {@link ZIndexable}. Provides fast element retrieval via hash key in addition to |
24 | | - * mutable ordering (z indexing) of elements. |
25 | | - */ |
26 | | -public class ZHash<KeyType, ValueType> implements ZIndexable<KeyType> { |
27 | | - |
28 | | - private HashMap<KeyType, ValueType> hash; |
29 | | - private ZLinkedList<KeyType> zlist; |
30 | | - |
31 | | - { |
32 | | - hash = new HashMap<>(); |
33 | | - zlist = new ZLinkedList<>(); |
34 | | - } |
35 | | - |
36 | | - public int size() { |
37 | | - return zlist.size(); |
38 | | - } |
39 | | - |
40 | | - |
41 | | - public ValueType get(KeyType key) { |
42 | | - return hash.get(key); |
43 | | - } |
44 | | - |
45 | | - public List<KeyType> getKeysAsList() { |
46 | | - return zlist; |
47 | | - } |
48 | | - |
49 | | - /** |
50 | | - * If key already exists within the structure, it's value is replaced with the new value and |
51 | | - * it's existing order is maintained. |
52 | | - * @param key |
53 | | - * @param value |
54 | | - */ |
55 | | - public synchronized void addToTop(KeyType key, ValueType value) { |
56 | | - if(hash.containsKey(key)) { |
57 | | - hash.put(key, value); |
58 | | - } else { |
59 | | - hash.put(key, value); |
60 | | - zlist.addToTop(key); |
61 | | - } |
62 | | - } |
63 | | - |
64 | | - /** |
65 | | - * If key already exists within the structure, it's value is replaced with the new value and |
66 | | - * it's existing order is maintained. |
67 | | - * @param key |
68 | | - * @param value |
69 | | - */ |
70 | | - public synchronized void addToBottom(KeyType key, ValueType value) { |
71 | | - if(hash.containsKey(key)) { |
72 | | - hash.put(key, value); |
73 | | - } else { |
74 | | - hash.put(key, value); |
75 | | - zlist.addToBottom(key); |
76 | | - } |
77 | | - } |
78 | | - |
79 | | - public synchronized boolean moveToTop(KeyType element) { |
80 | | - if(!hash.containsKey(element)) { |
81 | | - return false; |
82 | | - } else { |
83 | | - return zlist.moveToTop(element); |
84 | | - } |
85 | | - } |
86 | | - |
87 | | - public synchronized boolean moveAbove(KeyType objectToMove, KeyType reference) { |
88 | | - if(objectToMove == reference) { |
89 | | - throw new IllegalArgumentException("Illegal argument to moveAbove(A, B); A cannot be equal to B."); |
90 | | - } |
91 | | - if(!hash.containsKey(reference) || !hash.containsKey(objectToMove)) { |
92 | | - return false; |
93 | | - } else { |
94 | | - return zlist.moveAbove(objectToMove, reference); |
95 | | - } |
96 | | - } |
97 | | - |
98 | | - public synchronized boolean moveBeneath(KeyType objectToMove, KeyType reference) { |
99 | | - if(objectToMove == reference) { |
100 | | - throw new IllegalArgumentException("Illegal argument to moveBeaneath(A, B); A cannot be equal to B."); |
101 | | - } |
102 | | - if(!hash.containsKey(reference) || !hash.containsKey(objectToMove)) { |
103 | | - return false; |
104 | | - } else { |
105 | | - return zlist.moveBeneath(objectToMove, reference); |
106 | | - } |
107 | | - } |
108 | | - |
109 | | - public synchronized boolean moveToBottom(KeyType key) { |
110 | | - if(!hash.containsKey(key)) { |
111 | | - return false; |
112 | | - } else { |
113 | | - return zlist.moveToBottom(key); |
114 | | - } |
115 | | - } |
116 | | - |
117 | | - public synchronized boolean moveUp(KeyType key) { |
118 | | - if (!hash.containsKey(key)) { |
119 | | - return false; |
120 | | - } else { |
121 | | - return zlist.moveUp(key); |
122 | | - } |
123 | | - } |
124 | | - |
125 | | - public synchronized boolean moveDown(KeyType key) { |
126 | | - if (!hash.containsKey(key)) { |
127 | | - return false; |
128 | | - } else { |
129 | | - return zlist.moveDown(key); |
130 | | - } |
131 | | - } |
132 | | - |
133 | | - @Override |
134 | | - public List<KeyType> elements() { |
135 | | - return zlist; |
136 | | - } |
137 | | - |
138 | | - /** |
139 | | - * |
140 | | - * @return Ordered list of keys. |
141 | | - */ |
142 | | - public List<KeyType> keys() { |
143 | | - return elements(); |
144 | | - } |
145 | | - |
146 | | - |
147 | | - public synchronized boolean remove(KeyType key) { |
148 | | - if(hash.containsKey(key)) { |
149 | | - hash.remove(key); |
150 | | - zlist.remove(key); |
151 | | - return true; |
152 | | - } else { |
153 | | - return false; |
154 | | - } |
155 | | - } |
156 | | -} |
| 1 | +/* |
| 2 | + * Copyright 2015 AndroidPlot.com |
| 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 com.androidplot.util; |
| 18 | + |
| 19 | +import java.util.HashMap; |
| 20 | +import java.util.List; |
| 21 | + |
| 22 | +/** |
| 23 | + * An implementation of {@link Layerable}. Provides fast element retrieval via hash key in addition to |
| 24 | + * mutable ordering (z indexing) of elements. |
| 25 | + */ |
| 26 | +public class LayerHash<KeyType, ValueType> implements Layerable<KeyType> { |
| 27 | + |
| 28 | + private HashMap<KeyType, ValueType> hash; |
| 29 | + private LinkedLayerList<KeyType> zlist; |
| 30 | + |
| 31 | + { |
| 32 | + hash = new HashMap<>(); |
| 33 | + zlist = new LinkedLayerList<>(); |
| 34 | + } |
| 35 | + |
| 36 | + public int size() { |
| 37 | + return zlist.size(); |
| 38 | + } |
| 39 | + |
| 40 | + |
| 41 | + public ValueType get(KeyType key) { |
| 42 | + return hash.get(key); |
| 43 | + } |
| 44 | + |
| 45 | + public List<KeyType> getKeysAsList() { |
| 46 | + return zlist; |
| 47 | + } |
| 48 | + |
| 49 | + /** |
| 50 | + * If key already exists within the structure, it's value is replaced with the new value and |
| 51 | + * it's existing order is maintained. |
| 52 | + * @param key |
| 53 | + * @param value |
| 54 | + */ |
| 55 | + public synchronized void addToTop(KeyType key, ValueType value) { |
| 56 | + if(hash.containsKey(key)) { |
| 57 | + hash.put(key, value); |
| 58 | + } else { |
| 59 | + hash.put(key, value); |
| 60 | + zlist.addToTop(key); |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + /** |
| 65 | + * If key already exists within the structure, it's value is replaced with the new value and |
| 66 | + * it's existing order is maintained. |
| 67 | + * @param key |
| 68 | + * @param value |
| 69 | + */ |
| 70 | + public synchronized void addToBottom(KeyType key, ValueType value) { |
| 71 | + if(hash.containsKey(key)) { |
| 72 | + hash.put(key, value); |
| 73 | + } else { |
| 74 | + hash.put(key, value); |
| 75 | + zlist.addToBottom(key); |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + public synchronized boolean moveToTop(KeyType element) { |
| 80 | + if(!hash.containsKey(element)) { |
| 81 | + return false; |
| 82 | + } else { |
| 83 | + return zlist.moveToTop(element); |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + public synchronized boolean moveAbove(KeyType objectToMove, KeyType reference) { |
| 88 | + if(objectToMove == reference) { |
| 89 | + throw new IllegalArgumentException("Illegal argument to moveAbove(A, B); A cannot be equal to B."); |
| 90 | + } |
| 91 | + if(!hash.containsKey(reference) || !hash.containsKey(objectToMove)) { |
| 92 | + return false; |
| 93 | + } else { |
| 94 | + return zlist.moveAbove(objectToMove, reference); |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + public synchronized boolean moveBeneath(KeyType objectToMove, KeyType reference) { |
| 99 | + if(objectToMove == reference) { |
| 100 | + throw new IllegalArgumentException("Illegal argument to moveBeaneath(A, B); A cannot be equal to B."); |
| 101 | + } |
| 102 | + if(!hash.containsKey(reference) || !hash.containsKey(objectToMove)) { |
| 103 | + return false; |
| 104 | + } else { |
| 105 | + return zlist.moveBeneath(objectToMove, reference); |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + public synchronized boolean moveToBottom(KeyType key) { |
| 110 | + if(!hash.containsKey(key)) { |
| 111 | + return false; |
| 112 | + } else { |
| 113 | + return zlist.moveToBottom(key); |
| 114 | + } |
| 115 | + } |
| 116 | + |
| 117 | + public synchronized boolean moveUp(KeyType key) { |
| 118 | + if (!hash.containsKey(key)) { |
| 119 | + return false; |
| 120 | + } else { |
| 121 | + return zlist.moveUp(key); |
| 122 | + } |
| 123 | + } |
| 124 | + |
| 125 | + public synchronized boolean moveDown(KeyType key) { |
| 126 | + if (!hash.containsKey(key)) { |
| 127 | + return false; |
| 128 | + } else { |
| 129 | + return zlist.moveDown(key); |
| 130 | + } |
| 131 | + } |
| 132 | + |
| 133 | + @Override |
| 134 | + public List<KeyType> elements() { |
| 135 | + return zlist; |
| 136 | + } |
| 137 | + |
| 138 | + /** |
| 139 | + * |
| 140 | + * @return Ordered list of keys. |
| 141 | + */ |
| 142 | + public List<KeyType> keys() { |
| 143 | + return elements(); |
| 144 | + } |
| 145 | + |
| 146 | + |
| 147 | + public synchronized boolean remove(KeyType key) { |
| 148 | + if(hash.containsKey(key)) { |
| 149 | + hash.remove(key); |
| 150 | + zlist.remove(key); |
| 151 | + return true; |
| 152 | + } else { |
| 153 | + return false; |
| 154 | + } |
| 155 | + } |
| 156 | +} |
0 commit comments