View Javadoc
1   package com.exsoinn.util.epf;
2   
3   import com.exsoinn.util.ForwardingImmutableSet;
4   import net.jcip.annotations.Immutable;
5   
6   import java.util.*;
7   import java.util.concurrent.ConcurrentHashMap;
8   import java.util.stream.Collectors;
9   
10  /**
11   * Encapsulates a set of elements to return from a {@link Context} search. As a performance optimization,
12   * instances of this class are cached based on the <strong>order</strong> of the elements in the set. This means
13   * that sets "elem1,elem2" and "elem2,elem1" will be treated as two distinct objects, even though it's the same set.
14   * We figured that if we sorted the set elements every time an instance of this class is requested, it might
15   * result in a performance penalty, which defeats the purpose of caching in the first place. Having a few duplicate
16   * elements here and there is better as opposed to having to sort on every invocation.
17   * Objects of this class are made unmodifiable by having this class extend {@link ForwardingImmutableSet}.
18   * Created by QuijadaJ on 5/3/2017.
19   */
20  @Immutable
21  public final class TargetElements extends ForwardingImmutableSet<String> {
22      private final static Map<String, TargetElements> cachedLists = new ConcurrentHashMap<>();
23      private final Set<String> targetElementsSet;
24  
25      private TargetElements(Set<String> pSet) {
26          super(pSet);
27          targetElementsSet = pSet;
28      }
29  
30  
31      public static TargetElements valueOf(String pTargetElems)
32              throws IllegalArgumentException {
33          if (null == pTargetElems) {
34              return null;
35          }
36          TargetElements cachedList = cachedLists.get(pTargetElems);
37          if (null == cachedList) {
38              final Set<String> elems = parseTargetElements(pTargetElems);
39              TargetElements newList = new TargetElements(elems);
40              cachedList = cachedLists.putIfAbsent(pTargetElems, newList);
41              if (null == cachedList) {
42                  cachedList = newList;
43              }
44          }
45  
46          return cachedList;
47      }
48  
49      public static TargetElements fromSet(Set<String> pTargetElems) {
50          if (null == pTargetElems) {
51              return null;
52          }
53          return TargetElements.valueOf(pTargetElems.parallelStream().collect(Collectors.joining(",")));
54      }
55  
56  
57      private static Set<String> parseTargetElements(String pTargetElems) throws IllegalArgumentException{
58          String[] tokens = pTargetElems.split(",");
59          if (null == tokens) {
60              return null;
61          }
62          Set<String> elems = new HashSet<>();
63  
64          for (String t : tokens) {
65              elems.add(t);
66          }
67  
68          return elems;
69      }
70  
71  
72  
73      @Override
74      public String toString() {
75          StringBuilder sb = new StringBuilder();
76          sb.append(targetElementsSet.parallelStream().collect(Collectors.joining(",")));
77          return sb.toString();
78      }
79  }