blob: 520b9a62ae6befc2bb102e7cbd4472745d6c6ba2 [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
18import java.util.function.Function;
19
20import org.onosproject.store.service.AsyncConsistentMap;
Aaron Kruglikov61582a02016-09-06 13:18:58 -070021import org.onosproject.store.service.AsyncConsistentMultimap;
Aaron Kruglikoved88ff62016-08-01 16:02:09 -070022import org.onosproject.store.service.AsyncConsistentTreeMap;
Madan Jampani551d0d22016-02-01 12:51:48 -080023import org.onosproject.store.service.AsyncDistributedSet;
24
25/**
26 * Misc utilities for working with {@code DistributedPrimitive}s.
27 */
28public final class DistributedPrimitives {
29
30 private DistributedPrimitives() {}
31
32 /**
33 * Creates an instance of {@code AsyncDistributedSet} that is backed by a {@code AsyncConsistentMap}.
34 *
35 * @param map backing map
36 * @return set
37 * @param <E> set element type
38 */
39 public static <E> AsyncDistributedSet<E> newSetFromMap(AsyncConsistentMap<E, Boolean> map) {
40 return new DefaultAsyncDistributedSet<>(map, map.name(), true);
41 }
42
43 /**
44 * Creates an instance of {@code AsyncConsistentMap} that records metrics for all its operations.
45 *
46 * @param map map whose operations are to be metered
47 * @return metered map
48 * @param <K> map key type
49 * @param <V> map value type
50 */
51 public static <K, V> AsyncConsistentMap<K, V> newMeteredMap(AsyncConsistentMap<K, V> map) {
52 return new MeteredAsyncConsistentMap<>(map);
53 }
54
55 /**
56 * Creates an instance of {@code AsyncConsistentMap} that caches entries on get.
57 *
58 * @param map backing map
59 * @return caching map
60 * @param <K> map key type
61 * @param <V> map value type
62 */
63 public static <K, V> AsyncConsistentMap<K, V> newCachingMap(AsyncConsistentMap<K, V> map) {
64 return new CachingAsyncConsistentMap<>(map);
65 }
66
67 /**
68 * Creates an instance of {@code AsyncConsistentMap} that disallows updates.
69 *
70 * @param map backing map
71 * @return unmodifiable map
72 * @param <K> map key type
73 * @param <V> map value type
74 */
75 public static <K, V> AsyncConsistentMap<K, V> newUnmodifiableMap(AsyncConsistentMap<K, V> map) {
76 return new UnmodifiableAsyncConsistentMap<>(map);
77 }
78
79 /**
80 * Creates an instance of {@code AsyncConsistentMap} that transforms operations inputs and applies them
81 * to corresponding operation in a different typed map and returns the output after reverse transforming it.
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 valueEncoder transformer for value type of returned map to value type of input map
87 * @param valueDecoder transformer for value type of input map to value type of returned map
88 * @param <K1> returned map key type
89 * @param <K2> input map key type
90 * @param <V1> returned map value type
91 * @param <V2> input map key type
92 * @return new map
93 */
94 public static <K1, V1, K2, V2> AsyncConsistentMap<K1, V1> newTranscodingMap(AsyncConsistentMap<K2, V2> map,
95 Function<K1, K2> keyEncoder,
96 Function<K2, K1> keyDecoder,
97 Function<V1, V2> valueEncoder,
98 Function<V2, V1> valueDecoder) {
99 return new TranscodingAsyncConsistentMap<K1, V1, K2, V2>(map,
100 keyEncoder,
101 keyDecoder,
102 valueEncoder,
103 valueDecoder);
104 }
Aaron Kruglikoved88ff62016-08-01 16:02:09 -0700105
106 /**
107 * Creates an instance of {@code DistributedTreeMap} that transforms operations inputs and applies them
108 * to corresponding operation in a different typed map and returns the output after reverse transforming it.
109 *
110 * @param map backing map
111 * @param valueEncoder transformer for value type of returned map to value type of input map
112 * @param valueDecoder transformer for value type of input map to value type of returned map
113 * @param <V1> returned map value type
114 * @param <V2> input map key type
115 * @return new map
116 */
117 public static <V1, V2> AsyncConsistentTreeMap<V1> newTranscodingTreeMap(
118 AsyncConsistentTreeMap<V2> map,
119 Function<V1, V2> valueEncoder,
120 Function<V2, V1> valueDecoder) {
121 return new TranscodingAsyncConsistentTreeMap<>(map,
122 valueEncoder,
123 valueDecoder);
124 }
Aaron Kruglikov61582a02016-09-06 13:18:58 -0700125
126 /**
127 * Creates an instance of {@code AsyncConsistentMultimap} that transforms
128 * operations inputs and applies them to corresponding operation in a
129 * differently typed map and returns the output after reverse transforming
130 * it.
131 *
132 * @param multimap backing multimap
133 * @param keyEncoder transformer for key type of returned map to key type
134 * of input map
135 * @param keyDecoder transformer for key type of input map to key type of
136 * returned map
137 * @param valueEncoder transformer for value type of returned map to value
138 * type of input map
139 * @param valueDecoder transformer for value type of input map to value
140 * type of returned map
141 * @param <K1> returned map key type
142 * @param <K2> input map key type
143 * @param <V1> returned map value type
144 * @param <V2> input map key type
145 * @return new map
146 */
147 public static <K1, V1, K2, V2> AsyncConsistentMultimap<K1, V1>
148 newTranscodingMultimap(AsyncConsistentMultimap<K2, V2> multimap,
149 Function<K1, K2> keyEncoder,
150 Function<K2, K1> keyDecoder,
151 Function<V1, V2> valueEncoder,
152 Function<V2, V1> valueDecoder) {
153 return new TranscodingAsyncConsistentMultimap<>(multimap,
154 keyEncoder,
155 keyDecoder,
156 valueDecoder,
157 valueEncoder);
158 }
159
Madan Jampani551d0d22016-02-01 12:51:48 -0800160}