blob: 83af62d04de5c08bdff80e25700ece0fc528c73b [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 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
24/**
25 * Misc utilities for working with {@code DistributedPrimitive}s.
26 */
27public final class DistributedPrimitives {
28
29 private DistributedPrimitives() {}
30
31 /**
32 * Creates an instance of {@code AsyncDistributedSet} that is backed by a {@code AsyncConsistentMap}.
33 *
34 * @param map backing map
35 * @return set
36 * @param <E> set element type
37 */
38 public static <E> AsyncDistributedSet<E> newSetFromMap(AsyncConsistentMap<E, Boolean> map) {
39 return new DefaultAsyncDistributedSet<>(map, map.name(), true);
40 }
41
42 /**
43 * Creates an instance of {@code AsyncConsistentMap} that records metrics for all its operations.
44 *
45 * @param map map whose operations are to be metered
46 * @return metered map
47 * @param <K> map key type
48 * @param <V> map value type
49 */
50 public static <K, V> AsyncConsistentMap<K, V> newMeteredMap(AsyncConsistentMap<K, V> map) {
51 return new MeteredAsyncConsistentMap<>(map);
52 }
53
54 /**
55 * Creates an instance of {@code AsyncConsistentMap} that caches entries on get.
56 *
57 * @param map backing map
58 * @return caching map
59 * @param <K> map key type
60 * @param <V> map value type
61 */
62 public static <K, V> AsyncConsistentMap<K, V> newCachingMap(AsyncConsistentMap<K, V> map) {
63 return new CachingAsyncConsistentMap<>(map);
64 }
65
66 /**
67 * Creates an instance of {@code AsyncConsistentMap} that disallows updates.
68 *
69 * @param map backing map
70 * @return unmodifiable map
71 * @param <K> map key type
72 * @param <V> map value type
73 */
74 public static <K, V> AsyncConsistentMap<K, V> newUnmodifiableMap(AsyncConsistentMap<K, V> map) {
75 return new UnmodifiableAsyncConsistentMap<>(map);
76 }
77
78 /**
79 * Creates an instance of {@code AsyncConsistentMap} that transforms operations inputs and applies them
80 * to corresponding operation in a different typed map and returns the output after reverse transforming it.
81 *
82 * @param map backing map
83 * @param keyEncoder transformer for key type of returned map to key type of input map
84 * @param keyDecoder transformer for key type of input map to key type of returned map
85 * @param valueEncoder transformer for value type of returned map to value type of input map
86 * @param valueDecoder transformer for value type of input map to value type of returned map
87 * @param <K1> returned map key type
88 * @param <K2> input map key type
89 * @param <V1> returned map value type
90 * @param <V2> input map key type
91 * @return new map
92 */
93 public static <K1, V1, K2, V2> AsyncConsistentMap<K1, V1> newTranscodingMap(AsyncConsistentMap<K2, V2> map,
94 Function<K1, K2> keyEncoder,
95 Function<K2, K1> keyDecoder,
96 Function<V1, V2> valueEncoder,
97 Function<V2, V1> valueDecoder) {
98 return new TranscodingAsyncConsistentMap<K1, V1, K2, V2>(map,
99 keyEncoder,
100 keyDecoder,
101 valueEncoder,
102 valueDecoder);
103 }
Aaron Kruglikoved88ff62016-08-01 16:02:09 -0700104
105 /**
106 * Creates an instance of {@code DistributedTreeMap} that transforms operations inputs and applies them
107 * to corresponding operation in a different typed map and returns the output after reverse transforming it.
108 *
109 * @param map backing map
110 * @param valueEncoder transformer for value type of returned map to value type of input map
111 * @param valueDecoder transformer for value type of input map to value type of returned map
112 * @param <V1> returned map value type
113 * @param <V2> input map key type
114 * @return new map
115 */
116 public static <V1, V2> AsyncConsistentTreeMap<V1> newTranscodingTreeMap(
117 AsyncConsistentTreeMap<V2> map,
118 Function<V1, V2> valueEncoder,
119 Function<V2, V1> valueDecoder) {
120 return new TranscodingAsyncConsistentTreeMap<>(map,
121 valueEncoder,
122 valueDecoder);
123 }
Madan Jampani551d0d22016-02-01 12:51:48 -0800124}