blob: 42fb57c7afdd2dd781bd4f728e5943787b437dda [file] [log] [blame]
Madan Jampani551d0d22016-02-01 12:51:48 -08001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2016-present Open Networking Laboratory
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 Haltermanc955df72017-02-04 20:43:28 -080081 * Creates an instance of {@code AsyncAtomicCounterMap} that transforms key types.
82 *
83 * @param map backing map
84 * @param keyEncoder transformer for key type of returned map to key type of input map
85 * @param keyDecoder transformer for key type of input map to key type of returned map
86 * @param <K1> returned map key type
87 * @param <K2> input map key type
88 * @return new counter map
89 */
90 public static <K1, K2> AsyncAtomicCounterMap<K1> newTranscodingAtomicCounterMap(AsyncAtomicCounterMap<K2> map,
91 Function<K1, K2> keyEncoder,
92 Function<K2, K1> keyDecoder) {
93 return new TranscodingAsyncAtomicCounterMap<>(map, keyEncoder, keyDecoder);
94 }
95
96 /**
Madan Jampani551d0d22016-02-01 12:51:48 -080097 * Creates an instance of {@code AsyncConsistentMap} that transforms operations inputs and applies them
98 * to corresponding operation in a different typed map and returns the output after reverse transforming it.
99 *
100 * @param map backing map
101 * @param keyEncoder transformer for key type of returned map to key type of input map
102 * @param keyDecoder transformer for key type of input map to key type of returned map
103 * @param valueEncoder transformer for value type of returned map to value type of input map
104 * @param valueDecoder transformer for value type of input map to value type of returned map
105 * @param <K1> returned map key type
106 * @param <K2> input map key type
107 * @param <V1> returned map value type
108 * @param <V2> input map key type
109 * @return new map
110 */
111 public static <K1, V1, K2, V2> AsyncConsistentMap<K1, V1> newTranscodingMap(AsyncConsistentMap<K2, V2> map,
112 Function<K1, K2> keyEncoder,
113 Function<K2, K1> keyDecoder,
114 Function<V1, V2> valueEncoder,
115 Function<V2, V1> valueDecoder) {
116 return new TranscodingAsyncConsistentMap<K1, V1, K2, V2>(map,
117 keyEncoder,
118 keyDecoder,
119 valueEncoder,
120 valueDecoder);
121 }
Aaron Kruglikoved88ff62016-08-01 16:02:09 -0700122
123 /**
124 * Creates an instance of {@code DistributedTreeMap} that transforms operations inputs and applies them
125 * to corresponding operation in a different typed map and returns the output after reverse transforming it.
126 *
127 * @param map backing map
128 * @param valueEncoder transformer for value type of returned map to value type of input map
129 * @param valueDecoder transformer for value type of input map to value type of returned map
130 * @param <V1> returned map value type
131 * @param <V2> input map key type
132 * @return new map
133 */
134 public static <V1, V2> AsyncConsistentTreeMap<V1> newTranscodingTreeMap(
135 AsyncConsistentTreeMap<V2> map,
136 Function<V1, V2> valueEncoder,
137 Function<V2, V1> valueDecoder) {
138 return new TranscodingAsyncConsistentTreeMap<>(map,
139 valueEncoder,
140 valueDecoder);
141 }
Aaron Kruglikov61582a02016-09-06 13:18:58 -0700142
143 /**
144 * Creates an instance of {@code AsyncConsistentMultimap} that transforms
145 * operations inputs and applies them to corresponding operation in a
146 * differently typed map and returns the output after reverse transforming
147 * it.
148 *
149 * @param multimap backing multimap
150 * @param keyEncoder transformer for key type of returned map to key type
151 * of input map
152 * @param keyDecoder transformer for key type of input map to key type of
153 * returned map
154 * @param valueEncoder transformer for value type of returned map to value
155 * type of input map
156 * @param valueDecoder transformer for value type of input map to value
157 * type of returned map
158 * @param <K1> returned map key type
159 * @param <K2> input map key type
160 * @param <V1> returned map value type
161 * @param <V2> input map key type
162 * @return new map
163 */
164 public static <K1, V1, K2, V2> AsyncConsistentMultimap<K1, V1>
165 newTranscodingMultimap(AsyncConsistentMultimap<K2, V2> multimap,
166 Function<K1, K2> keyEncoder,
167 Function<K2, K1> keyDecoder,
168 Function<V1, V2> valueEncoder,
169 Function<V2, V1> valueDecoder) {
170 return new TranscodingAsyncConsistentMultimap<>(multimap,
171 keyEncoder,
172 keyDecoder,
173 valueDecoder,
174 valueEncoder);
175 }
176
Madan Jampani551d0d22016-02-01 12:51:48 -0800177}