blob: 2577ca0336df883e7631dfa67a654a1ab54ed79b [file] [log] [blame]
Madan Jampani551d0d22016-02-01 12:51:48 -08001/*
2 * Copyright 2016 Open Networking Laboratory
3 *
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;
21import org.onosproject.store.service.AsyncDistributedSet;
22
23/**
24 * Misc utilities for working with {@code DistributedPrimitive}s.
25 */
26public final class DistributedPrimitives {
27
28 private DistributedPrimitives() {}
29
30 /**
31 * Creates an instance of {@code AsyncDistributedSet} that is backed by a {@code AsyncConsistentMap}.
32 *
33 * @param map backing map
34 * @return set
35 * @param <E> set element type
36 */
37 public static <E> AsyncDistributedSet<E> newSetFromMap(AsyncConsistentMap<E, Boolean> map) {
38 return new DefaultAsyncDistributedSet<>(map, map.name(), true);
39 }
40
41 /**
42 * Creates an instance of {@code AsyncConsistentMap} that records metrics for all its operations.
43 *
44 * @param map map whose operations are to be metered
45 * @return metered map
46 * @param <K> map key type
47 * @param <V> map value type
48 */
49 public static <K, V> AsyncConsistentMap<K, V> newMeteredMap(AsyncConsistentMap<K, V> map) {
50 return new MeteredAsyncConsistentMap<>(map);
51 }
52
53 /**
54 * Creates an instance of {@code AsyncConsistentMap} that caches entries on get.
55 *
56 * @param map backing map
57 * @return caching map
58 * @param <K> map key type
59 * @param <V> map value type
60 */
61 public static <K, V> AsyncConsistentMap<K, V> newCachingMap(AsyncConsistentMap<K, V> map) {
62 return new CachingAsyncConsistentMap<>(map);
63 }
64
65 /**
66 * Creates an instance of {@code AsyncConsistentMap} that disallows updates.
67 *
68 * @param map backing map
69 * @return unmodifiable map
70 * @param <K> map key type
71 * @param <V> map value type
72 */
73 public static <K, V> AsyncConsistentMap<K, V> newUnmodifiableMap(AsyncConsistentMap<K, V> map) {
74 return new UnmodifiableAsyncConsistentMap<>(map);
75 }
76
77 /**
78 * Creates an instance of {@code AsyncConsistentMap} that transforms operations inputs and applies them
79 * to corresponding operation in a different typed map and returns the output after reverse transforming it.
80 *
81 * @param map backing map
82 * @param keyEncoder transformer for key type of returned map to key type of input map
83 * @param keyDecoder transformer for key type of input map to key type of returned map
84 * @param valueEncoder transformer for value type of returned map to value type of input map
85 * @param valueDecoder transformer for value type of input map to value type of returned map
86 * @param <K1> returned map key type
87 * @param <K2> input map key type
88 * @param <V1> returned map value type
89 * @param <V2> input map key type
90 * @return new map
91 */
92 public static <K1, V1, K2, V2> AsyncConsistentMap<K1, V1> newTranscodingMap(AsyncConsistentMap<K2, V2> map,
93 Function<K1, K2> keyEncoder,
94 Function<K2, K1> keyDecoder,
95 Function<V1, V2> valueEncoder,
96 Function<V2, V1> valueDecoder) {
97 return new TranscodingAsyncConsistentMap<K1, V1, K2, V2>(map,
98 keyEncoder,
99 keyDecoder,
100 valueEncoder,
101 valueDecoder);
102 }
103}