blob: d116988852c0326e99c70e4b70e114f9a6aa3fd2 [file] [log] [blame]
Madan Jampani551d0d22016-02-01 12:51:48 -08001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2016-present Open Networking Foundation
Madan Jampani551d0d22016-02-01 12:51:48 -08003 *
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 */
16package org.onosproject.store.primitives.impl;
17
Jordan Haltermanc955df72017-02-04 20:43:28 -080018import org.onosproject.store.service.AsyncAtomicCounterMap;
Madan Jampani551d0d22016-02-01 12:51:48 -080019import org.onosproject.store.service.AsyncConsistentMap;
Aaron Kruglikov61582a02016-09-06 13:18:58 -070020import org.onosproject.store.service.AsyncConsistentMultimap;
Aaron Kruglikoved88ff62016-08-01 16:02:09 -070021import org.onosproject.store.service.AsyncConsistentTreeMap;
Madan Jampani551d0d22016-02-01 12:51:48 -080022import org.onosproject.store.service.AsyncDistributedSet;
Jordan Halterman8d8da592017-08-28 14:45:19 -070023import org.onosproject.store.service.AsyncDocumentTree;
Madan Jampani551d0d22016-02-01 12:51:48 -080024
Jordan Haltermanc955df72017-02-04 20:43:28 -080025import java.util.function.Function;
26
Madan Jampani551d0d22016-02-01 12:51:48 -080027/**
28 * Misc utilities for working with {@code DistributedPrimitive}s.
29 */
30public final class DistributedPrimitives {
31
32 private DistributedPrimitives() {}
33
34 /**
35 * Creates an instance of {@code AsyncDistributedSet} that is backed by a {@code AsyncConsistentMap}.
36 *
37 * @param map backing map
38 * @return set
39 * @param <E> set element type
40 */
41 public static <E> AsyncDistributedSet<E> newSetFromMap(AsyncConsistentMap<E, Boolean> map) {
42 return new DefaultAsyncDistributedSet<>(map, map.name(), true);
43 }
44
45 /**
46 * Creates an instance of {@code AsyncConsistentMap} that records metrics for all its operations.
47 *
48 * @param map map whose operations are to be metered
49 * @return metered map
50 * @param <K> map key type
51 * @param <V> map value type
52 */
53 public static <K, V> AsyncConsistentMap<K, V> newMeteredMap(AsyncConsistentMap<K, V> map) {
54 return new MeteredAsyncConsistentMap<>(map);
55 }
56
57 /**
58 * Creates an instance of {@code AsyncConsistentMap} that caches entries on get.
59 *
60 * @param map backing map
61 * @return caching map
62 * @param <K> map key type
63 * @param <V> map value type
64 */
65 public static <K, V> AsyncConsistentMap<K, V> newCachingMap(AsyncConsistentMap<K, V> map) {
66 return new CachingAsyncConsistentMap<>(map);
67 }
68
69 /**
70 * Creates an instance of {@code AsyncConsistentMap} that disallows updates.
71 *
72 * @param map backing map
73 * @return unmodifiable map
74 * @param <K> map key type
75 * @param <V> map value type
76 */
77 public static <K, V> AsyncConsistentMap<K, V> newUnmodifiableMap(AsyncConsistentMap<K, V> map) {
78 return new UnmodifiableAsyncConsistentMap<>(map);
79 }
80
81 /**
Jordan Halterman4922a062017-07-31 15:55:36 -070082 * Creates an instance of {@code AsyncConsistentMap} that disallows null values.
83 *
84 * @param map backing map
85 * @return not null map
86 * @param <K> map key type
87 * @param <V> map value type
88 */
89 public static <K, V> AsyncConsistentMap<K, V> newNotNullMap(AsyncConsistentMap<K, V> map) {
90 return new NotNullAsyncConsistentMap<>(map);
91 }
92
93 /**
Jordan Haltermanc955df72017-02-04 20:43:28 -080094 * Creates an instance of {@code AsyncAtomicCounterMap} that transforms key types.
95 *
96 * @param map backing map
97 * @param keyEncoder transformer for key type of returned map to key type of input map
98 * @param keyDecoder transformer for key type of input map to key type of returned map
99 * @param <K1> returned map key type
100 * @param <K2> input map key type
101 * @return new counter map
102 */
103 public static <K1, K2> AsyncAtomicCounterMap<K1> newTranscodingAtomicCounterMap(AsyncAtomicCounterMap<K2> map,
104 Function<K1, K2> keyEncoder,
105 Function<K2, K1> keyDecoder) {
106 return new TranscodingAsyncAtomicCounterMap<>(map, keyEncoder, keyDecoder);
107 }
108
109 /**
Madan Jampani551d0d22016-02-01 12:51:48 -0800110 * Creates an instance of {@code AsyncConsistentMap} that transforms operations inputs and applies them
111 * to corresponding operation in a different typed map and returns the output after reverse transforming it.
112 *
113 * @param map backing map
114 * @param keyEncoder transformer for key type of returned map to key type of input map
115 * @param keyDecoder transformer for key type of input map to key type of returned map
116 * @param valueEncoder transformer for value type of returned map to value type of input map
117 * @param valueDecoder transformer for value type of input map to value type of returned map
118 * @param <K1> returned map key type
119 * @param <K2> input map key type
120 * @param <V1> returned map value type
121 * @param <V2> input map key type
122 * @return new map
123 */
124 public static <K1, V1, K2, V2> AsyncConsistentMap<K1, V1> newTranscodingMap(AsyncConsistentMap<K2, V2> map,
125 Function<K1, K2> keyEncoder,
126 Function<K2, K1> keyDecoder,
127 Function<V1, V2> valueEncoder,
128 Function<V2, V1> valueDecoder) {
129 return new TranscodingAsyncConsistentMap<K1, V1, K2, V2>(map,
130 keyEncoder,
131 keyDecoder,
132 valueEncoder,
133 valueDecoder);
134 }
Aaron Kruglikoved88ff62016-08-01 16:02:09 -0700135
136 /**
137 * Creates an instance of {@code DistributedTreeMap} that transforms operations inputs and applies them
138 * to corresponding operation in a different typed map and returns the output after reverse transforming it.
139 *
140 * @param map backing map
141 * @param valueEncoder transformer for value type of returned map to value type of input map
142 * @param valueDecoder transformer for value type of input map to value type of returned map
143 * @param <V1> returned map value type
144 * @param <V2> input map key type
145 * @return new map
146 */
147 public static <V1, V2> AsyncConsistentTreeMap<V1> newTranscodingTreeMap(
148 AsyncConsistentTreeMap<V2> map,
149 Function<V1, V2> valueEncoder,
150 Function<V2, V1> valueDecoder) {
151 return new TranscodingAsyncConsistentTreeMap<>(map,
152 valueEncoder,
153 valueDecoder);
154 }
Aaron Kruglikov61582a02016-09-06 13:18:58 -0700155
156 /**
157 * Creates an instance of {@code AsyncConsistentMultimap} that transforms
158 * operations inputs and applies them to corresponding operation in a
159 * differently typed map and returns the output after reverse transforming
160 * it.
161 *
162 * @param multimap backing multimap
163 * @param keyEncoder transformer for key type of returned map to key type
164 * of input map
165 * @param keyDecoder transformer for key type of input map to key type of
166 * returned map
167 * @param valueEncoder transformer for value type of returned map to value
168 * type of input map
169 * @param valueDecoder transformer for value type of input map to value
170 * type of returned map
171 * @param <K1> returned map key type
172 * @param <K2> input map key type
173 * @param <V1> returned map value type
174 * @param <V2> input map key type
175 * @return new map
176 */
177 public static <K1, V1, K2, V2> AsyncConsistentMultimap<K1, V1>
178 newTranscodingMultimap(AsyncConsistentMultimap<K2, V2> multimap,
179 Function<K1, K2> keyEncoder,
180 Function<K2, K1> keyDecoder,
181 Function<V1, V2> valueEncoder,
182 Function<V2, V1> valueDecoder) {
183 return new TranscodingAsyncConsistentMultimap<>(multimap,
184 keyEncoder,
185 keyDecoder,
186 valueDecoder,
187 valueEncoder);
188 }
189
Jordan Halterman8d8da592017-08-28 14:45:19 -0700190 /**
191 * Creates an instance of {@code AsyncDocumentTree} that caches values on get.
192 *
193 * @param tree backing tree
194 * @return caching tree
195 * @param <V> tree value type
196 */
197 public static <V> AsyncDocumentTree<V> newCachingDocumentTree(AsyncDocumentTree<V> tree) {
198 return new CachingAsyncDocumentTree<V>(tree);
199 }
200
Madan Jampani551d0d22016-02-01 12:51:48 -0800201}