Java Code Examples for java.util.AbstractSet

The following examples show how to use java.util.AbstractSet. 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: smithy   Source File: Model.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Converts the model to an immutable Set of shapes.
 *
 * @return Returns an unmodifiable set of shapes.
 */
public Set<Shape> toSet() {
    return new AbstractSet<Shape>() {
        @Override
        public int size() {
            return shapeMap.size();
        }

        @Override
        public boolean contains(Object o) {
            return o instanceof Shape && shapeMap.containsKey(((Shape) o).getId());
        }

        @Override
        public Iterator<Shape> iterator() {
            return shapeMap.values().iterator();
        }
    };
}
 
Example 2
Source Project: Bats   Source File: ImmutableBitSet.java    License: Apache License 2.0 6 votes vote down vote up
/** Creates a view onto this bit set as a set of integers.
 *
 * <p>The {@code size} and {@code contains} methods are both O(n), but the
 * iterator is efficient. */
public Set<Integer> asSet() {
  return new AbstractSet<Integer>() {
    public Iterator<Integer> iterator() {
      return ImmutableBitSet.this.iterator();
    }

    public int size() {
      return cardinality();
    }

    @Override public boolean contains(Object o) {
      return ImmutableBitSet.this.get((Integer) o);
    }
  };
}
 
Example 3
Source Project: Bats   Source File: Util.java    License: Apache License 2.0 6 votes vote down vote up
/** Returns a map that is a view onto a collection of values, using the
 * provided function to convert a value to a key.
 *
 * <p>Unlike
 * {@link com.google.common.collect.Maps#uniqueIndex(Iterable, com.google.common.base.Function)},
 * returns a view whose contents change as the collection of values changes.
 *
 * @param values Collection of values
 * @param function Function to map value to key
 * @param <K> Key type
 * @param <V> Value type
 * @return Map that is a view onto the values
 */
public static <K, V> Map<K, V> asIndexMapJ(
    final Collection<V> values,
    final Function<V, K> function) {
  final Collection<Map.Entry<K, V>> entries =
      Collections2.transform(values, v -> Pair.of(function.apply(v), v));
  final Set<Map.Entry<K, V>> entrySet =
      new AbstractSet<Map.Entry<K, V>>() {
        public Iterator<Map.Entry<K, V>> iterator() {
          return entries.iterator();
        }

        public int size() {
          return entries.size();
        }
      };
  return new AbstractMap<K, V>() {
    public Set<Entry<K, V>> entrySet() {
      return entrySet;
    }
  };
}
 
Example 4
Source Project: jdk1.8-source-analysis   Source File: Collectors.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public Set<Map.Entry<Boolean, T>> entrySet() {
    return new AbstractSet<Map.Entry<Boolean, T>>() {
        @Override
        public Iterator<Map.Entry<Boolean, T>> iterator() {
            Map.Entry<Boolean, T> falseEntry = new SimpleImmutableEntry<>(false, forFalse);
            Map.Entry<Boolean, T> trueEntry = new SimpleImmutableEntry<>(true, forTrue);
            return Arrays.asList(falseEntry, trueEntry).iterator();
        }

        @Override
        public int size() {
            return 2;
        }
    };
}
 
Example 5
Source Project: Quicksql   Source File: ImmutableBitSet.java    License: MIT License 6 votes vote down vote up
/** Creates a view onto this bit set as a set of integers.
 *
 * <p>The {@code size} and {@code contains} methods are both O(n), but the
 * iterator is efficient. */
public Set<Integer> asSet() {
  return new AbstractSet<Integer>() {
    @Nonnull public Iterator<Integer> iterator() {
      return ImmutableBitSet.this.iterator();
    }

    public int size() {
      return cardinality();
    }

    @Override public boolean contains(Object o) {
      return ImmutableBitSet.this.get((Integer) o);
    }
  };
}
 
Example 6
Source Project: Quicksql   Source File: Util.java    License: MIT License 6 votes vote down vote up
/** Returns a map that is a view onto a collection of values, using the
 * provided function to convert a value to a key.
 *
 * <p>Unlike
 * {@link com.google.common.collect.Maps#uniqueIndex(Iterable, com.google.common.base.Function)},
 * returns a view whose contents change as the collection of values changes.
 *
 * @param values Collection of values
 * @param function Function to map value to key
 * @param <K> Key type
 * @param <V> Value type
 * @return Map that is a view onto the values
 */
public static <K, V> Map<K, V> asIndexMapJ(
    final Collection<V> values,
    final Function<V, K> function) {
  final Collection<Map.Entry<K, V>> entries =
      Collections2.transform(values, v -> Pair.of(function.apply(v), v));
  final Set<Map.Entry<K, V>> entrySet =
      new AbstractSet<Map.Entry<K, V>>() {
        public Iterator<Map.Entry<K, V>> iterator() {
          return entries.iterator();
        }

        public int size() {
          return entries.size();
        }
      };
  return new AbstractMap<K, V>() {
    public Set<Entry<K, V>> entrySet() {
      return entrySet;
    }
  };
}
 
Example 7
Source Project: dragonwell8_jdk   Source File: Collectors.java    License: GNU General Public License v2.0 6 votes vote down vote up
@Override
public Set<Map.Entry<Boolean, T>> entrySet() {
    return new AbstractSet<Map.Entry<Boolean, T>>() {
        @Override
        public Iterator<Map.Entry<Boolean, T>> iterator() {
            Map.Entry<Boolean, T> falseEntry = new SimpleImmutableEntry<>(false, forFalse);
            Map.Entry<Boolean, T> trueEntry = new SimpleImmutableEntry<>(true, forTrue);
            return Arrays.asList(falseEntry, trueEntry).iterator();
        }

        @Override
        public int size() {
            return 2;
        }
    };
}
 
Example 8
Source Project: dragonwell8_jdk   Source File: Defaults.java    License: GNU General Public License v2.0 6 votes vote down vote up
public Set<Map.Entry<K, V>> entrySet() {
    return new AbstractSet<Map.Entry<K, V>>() {
        public int size() {
            return map.size();
        }

        public Iterator<Map.Entry<K,V>> iterator() {
            final Iterator<Map.Entry<K,V>> source = map.entrySet().iterator();
            return new Iterator<Map.Entry<K,V>>() {
               public boolean hasNext() { return source.hasNext(); }
               public Map.Entry<K,V> next() { return source.next(); }
               public void remove() { source.remove(); }
            };
        }

        public boolean add(Map.Entry<K,V> e) {
            return map.entrySet().add(e);
        }
    };
}
 
Example 9
Source Project: TencentKona-8   Source File: Collectors.java    License: GNU General Public License v2.0 6 votes vote down vote up
@Override
public Set<Map.Entry<Boolean, T>> entrySet() {
    return new AbstractSet<Map.Entry<Boolean, T>>() {
        @Override
        public Iterator<Map.Entry<Boolean, T>> iterator() {
            Map.Entry<Boolean, T> falseEntry = new SimpleImmutableEntry<>(false, forFalse);
            Map.Entry<Boolean, T> trueEntry = new SimpleImmutableEntry<>(true, forTrue);
            return Arrays.asList(falseEntry, trueEntry).iterator();
        }

        @Override
        public int size() {
            return 2;
        }
    };
}
 
Example 10
Source Project: TencentKona-8   Source File: Defaults.java    License: GNU General Public License v2.0 6 votes vote down vote up
public Set<Map.Entry<K, V>> entrySet() {
    return new AbstractSet<Map.Entry<K, V>>() {
        public int size() {
            return map.size();
        }

        public Iterator<Map.Entry<K,V>> iterator() {
            final Iterator<Map.Entry<K,V>> source = map.entrySet().iterator();
            return new Iterator<Map.Entry<K,V>>() {
               public boolean hasNext() { return source.hasNext(); }
               public Map.Entry<K,V> next() { return source.next(); }
               public void remove() { source.remove(); }
            };
        }

        public boolean add(Map.Entry<K,V> e) {
            return map.entrySet().add(e);
        }
    };
}
 
Example 11
Source Project: ZjDroid   Source File: ReflectionClassDef.java    License: Apache License 2.0 6 votes vote down vote up
@Nonnull @Override public Set<String> getInterfaces() {
    return new AbstractSet<String>() {
        @Nonnull @Override public Iterator<String> iterator() {
            return Iterators.transform(Iterators.forArray(cls.getInterfaces()), new Function<Class, String>() {
                @Nullable @Override public String apply(@Nullable Class input) {
                    if (input == null) {
                        return null;
                    }
                    return ReflectionUtils.javaToDexName(input.getName());
                }
            });
        }

        @Override public int size() {
            return cls.getInterfaces().length;
        }
    };
}
 
Example 12
Source Project: ZjDroid   Source File: ReflectionClassDef.java    License: Apache License 2.0 6 votes vote down vote up
@Nonnull @Override public Set<? extends Field> getFields() {
    return new AbstractSet<Field>() {
        @Nonnull @Override public Iterator<Field> iterator() {
            return Iterators.transform(Iterators.forArray(cls.getDeclaredFields()),
                    new Function<java.lang.reflect.Field, Field>() {
                        @Nullable @Override public Field apply(@Nullable java.lang.reflect.Field input) {
                            return new ReflectionField(input);
                        }
                    });
        }

        @Override public int size() {
            return cls.getDeclaredFields().length;
        }
    };
}
 
Example 13
Source Project: jdk8u60   Source File: Collectors.java    License: GNU General Public License v2.0 6 votes vote down vote up
@Override
public Set<Map.Entry<Boolean, T>> entrySet() {
    return new AbstractSet<Map.Entry<Boolean, T>>() {
        @Override
        public Iterator<Map.Entry<Boolean, T>> iterator() {
            Map.Entry<Boolean, T> falseEntry = new SimpleImmutableEntry<>(false, forFalse);
            Map.Entry<Boolean, T> trueEntry = new SimpleImmutableEntry<>(true, forTrue);
            return Arrays.asList(falseEntry, trueEntry).iterator();
        }

        @Override
        public int size() {
            return 2;
        }
    };
}
 
Example 14
Source Project: jdk8u60   Source File: Defaults.java    License: GNU General Public License v2.0 6 votes vote down vote up
public Set<Map.Entry<K, V>> entrySet() {
    return new AbstractSet<Map.Entry<K, V>>() {
        public int size() {
            return map.size();
        }

        public Iterator<Map.Entry<K,V>> iterator() {
            final Iterator<Map.Entry<K,V>> source = map.entrySet().iterator();
            return new Iterator<Map.Entry<K,V>>() {
               public boolean hasNext() { return source.hasNext(); }
               public Map.Entry<K,V> next() { return source.next(); }
               public void remove() { source.remove(); }
            };
        }

        public boolean add(Map.Entry<K,V> e) {
            return map.entrySet().add(e);
        }
    };
}
 
Example 15
Source Project: JDKSourceCode1.8   Source File: Collectors.java    License: MIT License 6 votes vote down vote up
@Override
public Set<Map.Entry<Boolean, T>> entrySet() {
    return new AbstractSet<Map.Entry<Boolean, T>>() {
        @Override
        public Iterator<Map.Entry<Boolean, T>> iterator() {
            Map.Entry<Boolean, T> falseEntry = new SimpleImmutableEntry<>(false, forFalse);
            Map.Entry<Boolean, T> trueEntry = new SimpleImmutableEntry<>(true, forTrue);
            return Arrays.asList(falseEntry, trueEntry).iterator();
        }

        @Override
        public int size() {
            return 2;
        }
    };
}
 
Example 16
Source Project: transport   Source File: PrestoMap.java    License: BSD 2-Clause "Simplified" License 6 votes vote down vote up
public Set<StdData> keySet() {
  return new AbstractSet<StdData>() {
    @Override
    public Iterator<StdData> iterator() {
      return new Iterator<StdData>() {
        int i = -2;

        @Override
        public boolean hasNext() {
          return !(i + 2 == size() * 2);
        }

        @Override
        public StdData next() {
          i += 2;
          return PrestoWrapper.createStdData(readNativeValue(_keyType, _block, i), _keyType, _stdFactory);
        }
      };
    }

    @Override
    public int size() {
      return PrestoMap.this.size();
    }
  };
}
 
Example 17
Source Project: transport   Source File: GenericMap.java    License: BSD 2-Clause "Simplified" License 6 votes vote down vote up
@Override
public Set<StdData> keySet() {
  return new AbstractSet<StdData>() {
    @Override
    public Iterator<StdData> iterator() {
      return new Iterator<StdData>() {
        Iterator<Object> keySet = _map.keySet().iterator();

        @Override
        public boolean hasNext() {
          return keySet.hasNext();
        }

        @Override
        public StdData next() {
          return GenericWrapper.createStdData(keySet.next(), _keyType);
        }
      };
    }

    @Override
    public int size() {
      return _map.size();
    }
  };
}
 
Example 18
Source Project: transport   Source File: HiveMap.java    License: BSD 2-Clause "Simplified" License 6 votes vote down vote up
@Override
public Set<StdData> keySet() {
  return new AbstractSet<StdData>() {
    @Override
    public Iterator<StdData> iterator() {
      return new Iterator<StdData>() {
        Iterator mapKeyIterator = _mapObjectInspector.getMap(_object).keySet().iterator();

        @Override
        public boolean hasNext() {
          return mapKeyIterator.hasNext();
        }

        @Override
        public StdData next() {
          return HiveWrapper.createStdData(mapKeyIterator.next(), _keyObjectInspector, _stdFactory);
        }
      };
    }

    @Override
    public int size() {
      return HiveMap.this.size();
    }
  };
}
 
Example 19
Source Project: transport   Source File: AvroMap.java    License: BSD 2-Clause "Simplified" License 6 votes vote down vote up
@Override
public Set<StdData> keySet() {
  return new AbstractSet<StdData>() {
    @Override
    public Iterator<StdData> iterator() {
      return new Iterator<StdData>() {
        Iterator<Object> keySet = _map.keySet().iterator();
        @Override
        public boolean hasNext() {
          return keySet.hasNext();
        }

        @Override
        public StdData next() {
          return AvroWrapper.createStdData(keySet.next(), _keySchema);
        }
      };
    }

    @Override
    public int size() {
      return _map.size();
    }
  };
}
 
Example 20
Source Project: gyro   Source File: FileScope.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public Set<Entry<String, Object>> entrySet() {
    return new AbstractSet<Entry<String, Object>>() {

        @Override
        public void clear() {
            FileScope.this.clear();
        }

        @Override
        public Iterator<Map.Entry<String, Object>> iterator() {
            return FileScope.super.entrySet().iterator();
        }

        @Override
        public int size() {
            return FileScope.super.entrySet().size();
        }
    };
}
 
Example 21
Source Project: gyro   Source File: FileScope.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public Set<String> keySet() {
    return new AbstractSet<String>() {

        @Override
        public void clear() {
            FileScope.this.clear();
        }

        @Override
        public Iterator<String> iterator() {
            return FileScope.super.keySet().iterator();
        }

        @Override
        public int size() {
            return FileScope.super.keySet().size();
        }
    };
}
 
Example 22
Source Project: desugar_jdk_libs   Source File: Collectors.java    License: GNU General Public License v2.0 6 votes vote down vote up
@Override
public Set<Map.Entry<Boolean, T>> entrySet() {
    return new AbstractSet<Map.Entry<Boolean, T>>() {
        @Override
        public Iterator<Map.Entry<Boolean, T>> iterator() {
            Map.Entry<Boolean, T> falseEntry = new SimpleImmutableEntry<>(false, forFalse);
            Map.Entry<Boolean, T> trueEntry = new SimpleImmutableEntry<>(true, forTrue);
            return Arrays.asList(falseEntry, trueEntry).iterator();
        }

        @Override
        public int size() {
            return 2;
        }
    };
}
 
Example 23
Source Project: openjdk-jdk8u   Source File: Collectors.java    License: GNU General Public License v2.0 6 votes vote down vote up
@Override
public Set<Map.Entry<Boolean, T>> entrySet() {
    return new AbstractSet<Map.Entry<Boolean, T>>() {
        @Override
        public Iterator<Map.Entry<Boolean, T>> iterator() {
            Map.Entry<Boolean, T> falseEntry = new SimpleImmutableEntry<>(false, forFalse);
            Map.Entry<Boolean, T> trueEntry = new SimpleImmutableEntry<>(true, forTrue);
            return Arrays.asList(falseEntry, trueEntry).iterator();
        }

        @Override
        public int size() {
            return 2;
        }
    };
}
 
Example 24
Source Project: openjdk-jdk8u   Source File: Defaults.java    License: GNU General Public License v2.0 6 votes vote down vote up
public Set<Map.Entry<K, V>> entrySet() {
    return new AbstractSet<Map.Entry<K, V>>() {
        public int size() {
            return map.size();
        }

        public Iterator<Map.Entry<K,V>> iterator() {
            final Iterator<Map.Entry<K,V>> source = map.entrySet().iterator();
            return new Iterator<Map.Entry<K,V>>() {
               public boolean hasNext() { return source.hasNext(); }
               public Map.Entry<K,V> next() { return source.next(); }
               public void remove() { source.remove(); }
            };
        }

        public boolean add(Map.Entry<K,V> e) {
            return map.entrySet().add(e);
        }
    };
}
 
Example 25
Source Project: n4js   Source File: MemberByNameAndAccessMap.java    License: Eclipse Public License 1.0 6 votes vote down vote up
@Override
public Set<Map.Entry<NameAndAccess, TMember>> entrySet() {
	return new AbstractSet<>() {
		@Override
		public Iterator<Map.Entry<NameAndAccess, TMember>> iterator() {
			return Iterators.unmodifiableIterator(
					new TransformedIterator2<TMember, Map.Entry<NameAndAccess, TMember>>(members.iterator()) {
						@Override
						protected Map.Entry<NameAndAccess, TMember> transform(TMember input) {
							final NameAndAccess[] nameAndAccess = NameAndAccess.of(input);
							if (nameAndAccess.length > 1) {
								setAdditionalElement(
										new AbstractMap.SimpleImmutableEntry<>(nameAndAccess[1], input));
							}
							return new AbstractMap.SimpleImmutableEntry<>(nameAndAccess[0], input);
						}
					});
		}

		@Override
		public int size() {
			return members.size();
		}
	};
}
 
Example 26
Source Project: neoscada   Source File: AbstractIoService.java    License: Eclipse Public License 1.0 6 votes vote down vote up
/**
 * {@inheritDoc}
 */
public final Set<WriteFuture> broadcast(Object message) {
    // Convert to Set.  We do not return a List here because only the
    // direct caller of MessageBroadcaster knows the order of write
    // operations.
    final List<WriteFuture> futures = IoUtil.broadcast(message, getManagedSessions().values());
    return new AbstractSet<WriteFuture>() {
        @Override
        public Iterator<WriteFuture> iterator() {
            return futures.iterator();
        }

        @Override
        public int size() {
            return futures.size();
        }
    };
}
 
Example 27
Source Project: org.alloytools.alloy   Source File: Containers.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Returns a set backed by the given array (i.e. a set that uses object equality
 * for comparisons). The array must contain no duplicates, its elements must be
 * sorted in the increasing order of hashcodes (as by
 * {@link #hashSort(Object[])}), and its contents must not be changed while it
 * is in use by the returned set.
 *
 * @requires all i, j: [0..array.length) | i < j => array[i].hashCode() <=
 *           array[j].hashCode
 * @return an unmodifiable Set view of the given array
 */
public static final <T> Set<T> asHashSet(final T[] array) {
    return new AbstractSet<T>() {

        @Override
        public boolean contains(Object o) {
            return hashBinarySearch(array, o) >= 0;
        }

        @Override
        public Iterator<T> iterator() {
            return iterate(array);
        }

        @Override
        public int size() {
            return array.length;
        }
    };
}
 
Example 28
@Override
public Set<Map.Entry<Boolean, T>> entrySet() {
    return new AbstractSet<Map.Entry<Boolean, T>>() {
        @Override
        public Iterator<Map.Entry<Boolean, T>> iterator() {
            Map.Entry<Boolean, T> falseEntry = new SimpleImmutableEntry<>(false, forFalse);
            Map.Entry<Boolean, T> trueEntry = new SimpleImmutableEntry<>(true, forTrue);
            return Arrays.asList(falseEntry, trueEntry).iterator();
        }

        @Override
        public int size() {
            return 2;
        }
    };
}
 
Example 29
public Set<Map.Entry<K, V>> entrySet() {
    return new AbstractSet<Map.Entry<K, V>>() {
        public int size() {
            return map.size();
        }

        public Iterator<Map.Entry<K,V>> iterator() {
            final Iterator<Map.Entry<K,V>> source = map.entrySet().iterator();
            return new Iterator<Map.Entry<K,V>>() {
               public boolean hasNext() { return source.hasNext(); }
               public Map.Entry<K,V> next() { return source.next(); }
               public void remove() { source.remove(); }
            };
        }

        public boolean add(Map.Entry<K,V> e) {
            return map.entrySet().add(e);
        }
    };
}
 
Example 30
Source Project: Bytecoder   Source File: Collectors.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public Set<Map.Entry<Boolean, T>> entrySet() {
    return new AbstractSet<>() {
        @Override
        public Iterator<Map.Entry<Boolean, T>> iterator() {
            Map.Entry<Boolean, T> falseEntry = new SimpleImmutableEntry<>(false, forFalse);
            Map.Entry<Boolean, T> trueEntry = new SimpleImmutableEntry<>(true, forTrue);
            return List.of(falseEntry, trueEntry).iterator();
        }

        @Override
        public int size() {
            return 2;
        }
    };
}