Java Code Examples for java.util.Collections#unmodifiableNavigableSet()

The following examples show how to use java.util.Collections#unmodifiableNavigableSet() . These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source Project: hadoop-ozone   File: ContainerAttribute.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Returns the collection that maps to the given key.
 *
 * @param key - Key to the bucket.
 * @return Underlying Set in immutable form.
 */
public NavigableSet<ContainerID> getCollection(T key) {
  Preconditions.checkNotNull(key);

  if (this.attributeMap.containsKey(key)) {
    return Collections.unmodifiableNavigableSet(this.attributeMap.get(key));
  }
  if (LOG.isDebugEnabled()) {
    LOG.debug("No such Key. Key {}", key);
  }
  return EMPTY_SET;
}
 
Example 2
public CollectionFields build() {
  List<String> _strings = (strings != null) ? Collections.unmodifiableList(new ArrayList<String>(strings)) : Collections.<String>emptyList();
  Map<String, Integer> _integers = (integers != null) ? Collections.unmodifiableMap(new HashMap<String, Integer>(integers)) : Collections.<String, Integer>emptyMap();
  SortedMap<String, Integer> _sortedIntegers = (sortedIntegers != null) ? Collections.unmodifiableSortedMap(new TreeMap<String, Integer>(sortedIntegers)) : Collections.<String, Integer>emptySortedMap();
  NavigableMap<String, Integer> _navigableIntegers = (navigableIntegers != null) ? Collections.unmodifiableNavigableMap(new TreeMap<String, Integer>(navigableIntegers)) : Collections.<String, Integer>emptyNavigableMap();
  Set<Long> _numbers = (numbers != null) ? Collections.unmodifiableSet(new HashSet<Long>(numbers)) : Collections.<Long>emptySet();
  SortedSet<Long> _sortedNumbers = (sortedNumbers != null) ? Collections.unmodifiableSortedSet(new TreeSet<Long>(sortedNumbers)) : Collections.<Long>emptySortedSet();
  NavigableSet<Long> _navigableNumbers = (navigableNumbers != null) ? Collections.unmodifiableNavigableSet(new TreeSet<Long>(navigableNumbers)) : Collections.<Long>emptyNavigableSet();
  return new Value(_strings, _integers, _sortedIntegers, _navigableIntegers, _numbers, _sortedNumbers, _navigableNumbers);
}
 
Example 3
Source Project: j2objc   File: CollectionsTest.java    License: Apache License 2.0 5 votes vote down vote up
public void test_unmodifiableNavigableSet_nonEmpty() {
    NavigableSet<String> delegate = new TreeSet<>();
    NavigableSet<String> set = Collections.unmodifiableNavigableSet(delegate);
    delegate.add("pear");
    delegate.add("banana");
    delegate.add("apple");
    delegate.add("melon");

    check_unmodifiableNavigableSet(set,
            Arrays.asList("apple", "banana", "melon", "pear"),
            "absent element");

    assertEquals("pear", set.ceiling("nonexistent"));
    assertEquals("melon", set.floor("nonexistent"));
}
 
Example 4
Source Project: ph-commons   File: CollectionHelper.java    License: Apache License 2.0 5 votes vote down vote up
@Nullable
@ReturnsImmutableObject
@CodingStyleguideUnaware
public static <ELEMENTTYPE> NavigableSet <ELEMENTTYPE> makeUnmodifiable (@Nullable final NavigableSet <ELEMENTTYPE> aNavigableSet)
{
  return aNavigableSet == null ? null : Collections.unmodifiableNavigableSet (aNavigableSet);
}
 
Example 5
Source Project: ph-commons   File: CollectionHelper.java    License: Apache License 2.0 5 votes vote down vote up
@Nonnull
@ReturnsImmutableObject
@CodingStyleguideUnaware
public static <ELEMENTTYPE extends Comparable <? super ELEMENTTYPE>> NavigableSet <ELEMENTTYPE> makeUnmodifiableNotNull (@Nullable final NavigableSet <ELEMENTTYPE> aNavigableSet)
{
  return aNavigableSet == null ? Collections.emptyNavigableSet ()
                               : Collections.unmodifiableNavigableSet (aNavigableSet);
}
 
Example 6
Source Project: ph-commons   File: ICommonsNavigableSet.java    License: Apache License 2.0 5 votes vote down vote up
@Override
@Nonnull
@CodingStyleguideUnaware
default NavigableSet <ELEMENTTYPE> getAsUnmodifiable ()
{
  return Collections.unmodifiableNavigableSet (this);
}
 
Example 7
Source Project: Diorite   File: ConfigPropertyTemplateImpl.java    License: MIT License 4 votes vote down vote up
@Nullable
@Override
public T get(ConfigPropertyValue<T> propertyValue)
{
    T rawValue = propertyValue.getRawValue();
    if (rawValue == null)
    {
        return null;
    }
    if (this.returnUnmodifiableCollections)
    {
        if (rawValue instanceof Collection)
        {
            if (rawValue instanceof Set)
            {
                if (rawValue instanceof NavigableSet)
                {
                    return (T) Collections.unmodifiableNavigableSet((NavigableSet<?>) rawValue);
                }
                if (rawValue instanceof SortedSet)
                {
                    return (T) Collections.unmodifiableSortedSet((SortedSet<?>) rawValue);
                }
                return (T) Collections.unmodifiableSet((Set<?>) rawValue);
            }
            if (rawValue instanceof List)
            {
                return (T) Collections.unmodifiableList((List<?>) rawValue);
            }
            return (T) Collections.unmodifiableCollection((Collection<?>) rawValue);
        }
        else if (rawValue instanceof Map)
        {
            if (rawValue instanceof NavigableMap)
            {
                return (T) Collections.unmodifiableNavigableMap((NavigableMap<?, ?>) rawValue);
            }
            if (rawValue instanceof SortedMap)
            {
                return (T) Collections.unmodifiableSortedMap((SortedMap<?, ?>) rawValue);
            }
            return (T) Collections.unmodifiableMap((Map<?, ?>) rawValue);
        }
    }
    return rawValue;
}
 
Example 8
Source Project: JDA   File: ReadWriteLockCache.java    License: Apache License 2.0 4 votes vote down vote up
protected NavigableSet<T> cache(NavigableSet<T> set)
{
    set = Collections.unmodifiableNavigableSet(set);
    cachedSet = new WeakReference<>(set);
    return set;
}
 
Example 9
Source Project: j2objc   File: CollectionsTest.java    License: Apache License 2.0 4 votes vote down vote up
public void test_unmodifiableNavigableSet_empty() {
    NavigableSet<String> set = Collections.unmodifiableNavigableSet(new TreeSet<>());
    check_unmodifiableSet(set, "absent element");
    check_navigableSet(set, new ArrayList<>(), "absent element");
}
 
Example 10
Source Project: ambry   File: IndexSegment.java    License: Apache License 2.0 4 votes vote down vote up
/**
 * Finds an entry given a key. It finds from the in memory map or
 * does a binary search on the mapped persistent segment
 * @param keyToFind The key to find
 * @return The blob index value that represents the key or null if not found
 * @throws StoreException
 */
NavigableSet<IndexValue> find(StoreKey keyToFind) throws StoreException {
  NavigableSet<IndexValue> toReturn = null;
  rwLock.readLock().lock();
  try {
    if (!sealed.get()) {
      ConcurrentSkipListSet<IndexValue> values = index.get(keyToFind);
      if (values != null) {
        metrics.blobFoundInMemSegmentCount.inc();
        toReturn = values.clone();
      }
    } else {
      if (bloomFilter != null) {
        metrics.bloomAccessedCount.inc();
      }
      if (bloomFilter == null || bloomFilter.isPresent(getStoreKeyBytes(keyToFind))) {
        if (bloomFilter == null) {
          logger.trace("IndexSegment {} bloom filter empty. Searching file with start offset {} and for key {}",
              indexFile.getAbsolutePath(), startOffset, keyToFind);
        } else {
          metrics.bloomPositiveCount.inc();
          logger.trace("IndexSegment {} found in bloom filter for index with start offset {} and for key {} ",
              indexFile.getAbsolutePath(), startOffset, keyToFind);
        }
        if (config.storeIndexMemState == IndexMemState.MMAP_WITH_FORCE_LOAD
            || config.storeIndexMemState == IndexMemState.MMAP_WITHOUT_FORCE_LOAD) {
          // isLoaded() will be true only if the entire buffer is in memory - so it being false does not necessarily
          // mean that the pages in the scope of the search need to be loaded from disk.
          // Secondly, even if it returned true (or false), by the time the actual lookup is done,
          // the situation may be different.
          if (((MappedByteBuffer) serEntries).isLoaded()) {
            metrics.mappedSegmentIsLoadedDuringFindCount.inc();
          } else {
            metrics.mappedSegmentIsNotLoadedDuringFindCount.inc();
          }
        }
        // binary search on the mapped file
        ByteBuffer duplicate = serEntries.duplicate();
        int low = 0;
        int totalEntries = numberOfEntries(duplicate);
        int high = totalEntries - 1;
        logger.trace("binary search low : {} high : {}", low, high);
        while (low <= high) {
          int mid = (int) (Math.ceil(high / 2.0 + low / 2.0));
          StoreKey found = getKeyAt(duplicate, mid);
          logger.trace("Index Segment {} binary search - key found on iteration {}", indexFile.getAbsolutePath(),
              found);
          int result = found.compareTo(keyToFind);
          if (result == 0) {
            toReturn = new TreeSet<>();
            getAllValuesFromMmap(duplicate, keyToFind, mid, totalEntries, toReturn);
            break;
          } else if (result < 0) {
            low = mid + 1;
          } else {
            high = mid - 1;
          }
        }
        if (bloomFilter != null && toReturn == null) {
          metrics.bloomFalsePositiveCount.inc();
        }
      }
    }
  } catch (StoreException e) {
    throw new StoreException(String.format("IndexSegment %s : %s", indexFile.getAbsolutePath(), e.getMessage()), e,
        e.getErrorCode());
  } finally {
    rwLock.readLock().unlock();
  }
  return toReturn != null ? Collections.unmodifiableNavigableSet(toReturn) : null;
}
 
Example 11
Source Project: Javacord   File: MessageSetImpl.java    License: Apache License 2.0 2 votes vote down vote up
/**
 * Creates a new message set.
 *
 * @param messages The messages to be contained in this set.
 */
public MessageSetImpl(NavigableSet<Message> messages) {
    this.messages = Collections.unmodifiableNavigableSet(messages);
}
 
Example 12
Source Project: Javacord   File: MessageSetImpl.java    License: Apache License 2.0 2 votes vote down vote up
/**
 * Creates a new message set.
 *
 * @param messages The messages to be contained in this set.
 */
public MessageSetImpl(NavigableSet<Message> messages) {
    this.messages = Collections.unmodifiableNavigableSet(messages);
}