blob: 33b72a4b3e4961530a7e77e1f4c5bc5e4a2d8ec8 [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;
23
Jordan Haltermanc955df72017-02-04 20:43:28 -080024import java.util.function.Function;
25
Madan Jampani551d0d22016-02-01 12:51:48 -080026/**
27 * Misc utilities for working with {@code DistributedPrimitive}s.
28 */
29public final class DistributedPrimitives {
30
31 private DistributedPrimitives() {}
32
33 /**
34 * Creates an instance of {@code AsyncDistributedSet} that is backed by a {@code AsyncConsistentMap}.
35 *
36 * @param map backing map
37 * @return set
38 * @param <E> set element type
39 */
40 public static <E> AsyncDistributedSet<E> newSetFromMap(AsyncConsistentMap<E, Boolean> map) {
41 return new DefaultAsyncDistributedSet<>(map, map.name(), true);
42 }
43
44 /**
45 * Creates an instance of {@code AsyncConsistentMap} that records metrics for all its operations.
46 *
47 * @param map map whose operations are to be metered
48 * @return metered map
49 * @param <K> map key type
50 * @param <V> map value type
51 */
52 public static <K, V> AsyncConsistentMap<K, V> newMeteredMap(AsyncConsistentMap<K, V> map) {
53 return new MeteredAsyncConsistentMap<>(map);
54 }
55
56 /**
57 * Creates an instance of {@code AsyncConsistentMap} that caches entries on get.
58 *
59 * @param map backing map
60 * @return caching map
61 * @param <K> map key type
62 * @param <V> map value type
63 */
64 public static <K, V> AsyncConsistentMap<K, V> newCachingMap(AsyncConsistentMap<K, V> map) {
65 return new CachingAsyncConsistentMap<>(map);
66 }
67
68 /**
69 * Creates an instance of {@code AsyncConsistentMap} that disallows updates.
70 *
71 * @param map backing map
72 * @return unmodifiable map
73 * @param <K> map key type
74 * @param <V> map value type
75 */
76 public static <K, V> AsyncConsistentMap<K, V> newUnmodifiableMap(AsyncConsistentMap<K, V> map) {
77 return new UnmodifiableAsyncConsistentMap<>(map);
78 }
79
80 /**
Jordan Halterman4922a062017-07-31 15:55:36 -070081 * Creates an instance of {@code AsyncConsistentMap} that disallows null values.
82 *
83 * @param map backing map
84 * @return not null map
85 * @param <K> map key type
86 * @param <V> map value type
87 */
88 public static <K, V> AsyncConsistentMap<K, V> newNotNullMap(AsyncConsistentMap<K, V> map) {
89 return new NotNullAsyncConsistentMap<>(map);
90 }
91
92 /**
Jordan Haltermanc955df72017-02-04 20:43:28 -080093 * Creates an instance of {@code AsyncAtomicCounterMap} that transforms key types.
94 *
95 * @param map backing map
96 * @param keyEncoder transformer for key type of returned map to key type of input map
97 * @param keyDecoder transformer for key type of input map to key type of returned map
98 * @param <K1> returned map key type
99 * @param <K2> input map key type
100 * @return new counter map
101 */
102 public static <K1, K2> AsyncAtomicCounterMap<K1> newTranscodingAtomicCounterMap(AsyncAtomicCounterMap<K2> map,
103 Function<K1, K2> keyEncoder,
104 Function<K2, K1> keyDecoder) {
105 return new TranscodingAsyncAtomicCounterMap<>(map, keyEncoder, keyDecoder);
106 }
107
108 /**
Madan Jampani551d0d22016-02-01 12:51:48 -0800109 * Creates an instance of {@code AsyncConsistentMap} that transforms operations inputs and applies them
110 * to corresponding operation in a different typed map and returns the output after reverse transforming it.
111 *
112 * @param map backing map
113 * @param keyEncoder transformer for key type of returned map to key type of input map
114 * @param keyDecoder transformer for key type of input map to key type of returned map
115 * @param valueEncoder transformer for value type of returned map to value type of input map
116 * @param valueDecoder transformer for value type of input map to value type of returned map
117 * @param <K1> returned map key type
118 * @param <K2> input map key type
119 * @param <V1> returned map value type
120 * @param <V2> input map key type
121 * @return new map
122 */
123 public static <K1, V1, K2, V2> AsyncConsistentMap<K1, V1> newTranscodingMap(AsyncConsistentMap<K2, V2> map,
124 Function<K1, K2> keyEncoder,
125 Function<K2, K1> keyDecoder,
126 Function<V1, V2> valueEncoder,
127 Function<V2, V1> valueDecoder) {
128 return new TranscodingAsyncConsistentMap<K1, V1, K2, V2>(map,
129 keyEncoder,
130 keyDecoder,
131 valueEncoder,
132 valueDecoder);
133 }
Aaron Kruglikoved88ff62016-08-01 16:02:09 -0700134
135 /**
136 * Creates an instance of {@code DistributedTreeMap} that transforms operations inputs and applies them
137 * to corresponding operation in a different typed map and returns the output after reverse transforming it.
138 *
139 * @param map backing map
140 * @param valueEncoder transformer for value type of returned map to value type of input map
141 * @param valueDecoder transformer for value type of input map to value type of returned map
142 * @param <V1> returned map value type
143 * @param <V2> input map key type
144 * @return new map
145 */
146 public static <V1, V2> AsyncConsistentTreeMap<V1> newTranscodingTreeMap(
147 AsyncConsistentTreeMap<V2> map,
148 Function<V1, V2> valueEncoder,
149 Function<V2, V1> valueDecoder) {
150 return new TranscodingAsyncConsistentTreeMap<>(map,
151 valueEncoder,
152 valueDecoder);
153 }
Aaron Kruglikov61582a02016-09-06 13:18:58 -0700154
155 /**
156 * Creates an instance of {@code AsyncConsistentMultimap} that transforms
157 * operations inputs and applies them to corresponding operation in a
158 * differently typed map and returns the output after reverse transforming
159 * it.
160 *
161 * @param multimap backing multimap
162 * @param keyEncoder transformer for key type of returned map to key type
163 * of input map
164 * @param keyDecoder transformer for key type of input map to key type of
165 * returned map
166 * @param valueEncoder transformer for value type of returned map to value
167 * type of input map
168 * @param valueDecoder transformer for value type of input map to value
169 * type of returned map
170 * @param <K1> returned map key type
171 * @param <K2> input map key type
172 * @param <V1> returned map value type
173 * @param <V2> input map key type
174 * @return new map
175 */
176 public static <K1, V1, K2, V2> AsyncConsistentMultimap<K1, V1>
177 newTranscodingMultimap(AsyncConsistentMultimap<K2, V2> multimap,
178 Function<K1, K2> keyEncoder,
179 Function<K2, K1> keyDecoder,
180 Function<V1, V2> valueEncoder,
181 Function<V2, V1> valueDecoder) {
182 return new TranscodingAsyncConsistentMultimap<>(multimap,
183 keyEncoder,
184 keyDecoder,
185 valueDecoder,
186 valueEncoder);
187 }
188
Madan Jampani551d0d22016-02-01 12:51:48 -0800189}