Java Code Examples for com.google.common.collect.ContiguousSet

The following are top voted examples for showing how to use com.google.common.collect.ContiguousSet. These examples are extracted from open source projects. You can vote up the examples you like and your votes will be used in our system to generate more good examples.
Example 1
Project: guava-mock   File: SetGenerators.java   Source Code and License 6 votes vote down vote up
@Override
protected SortedSet<Integer> create(Integer[] elements) {
  SortedSet<Integer> set = nullCheckedTreeSet(elements);
  if (set.isEmpty()) {
    /*
     * The (tooLow + 1, tooHigh) arguments below would be invalid because tooLow would be
     * greater than tooHigh.
     */
    return ContiguousSet.create(Range.openClosed(0, 1), DiscreteDomain.integers()).subSet(0, 1);
  }
  int tooHigh = set.last() + 1;
  int tooLow = set.first() - 1;
  set.add(tooHigh);
  set.add(tooLow);
  return checkedCreate(set).subSet(tooLow + 1, tooHigh);
}
 
Example 2
Project: es-sql   File: AggregationTest.java   Source Code and License 6 votes vote down vote up
@Test
public void multipleGroupByTest() throws Exception {
	Set expectedAges = new HashSet<Integer>(ContiguousSet.create(Range.closed(20, 40), DiscreteDomain.integers()));

	Map<String, Set<Integer>> buckets = new HashMap<>();

	Aggregations result = query(String.format("SELECT COUNT(*) FROM %s/account GROUP BY gender,  terms('field'='age','size'=200,'alias'='age')", TEST_INDEX));
	Terms gender = result.get("gender");
	for(Terms.Bucket genderBucket : gender.getBuckets()) {
		String genderKey = genderBucket.getKey().toString();
		buckets.put(genderKey, new HashSet<Integer>());
		Terms ageBuckets = (Terms) genderBucket.getAggregations().get("age");
		for(Terms.Bucket ageBucket : ageBuckets.getBuckets()) {
			buckets.get(genderKey).add(Integer.parseInt(ageBucket.getKey().toString()));
		}
	}

	Assert.assertEquals(2, buckets.keySet().size());
	Assert.assertEquals(expectedAges, buckets.get("m"));
	Assert.assertEquals(expectedAges, buckets.get("f"));
}
 
Example 3
Project: es-sql   File: AggregationTest.java   Source Code and License 6 votes vote down vote up
@Test
public void multipleGroupBysWithSize() throws Exception {
    Set expectedAges = new HashSet<Integer>(ContiguousSet.create(Range.closed(20, 40), DiscreteDomain.integers()));

    Map<String, Set<Integer>> buckets = new HashMap<>();

    Aggregations result = query(String.format("SELECT COUNT(*) FROM %s/account GROUP BY gender, terms('alias'='ageAgg','field'='age','size'=3)", TEST_INDEX));
    Terms gender = result.get("gender");
    Assert.assertEquals(2,gender.getBuckets().size());
    for(Terms.Bucket genderBucket : gender.getBuckets()) {

        String genderKey = genderBucket.getKey().toString();
        buckets.put(genderKey, new HashSet<Integer>());
        Terms ageBuckets = genderBucket.getAggregations().get("ageAgg");
        Assert.assertEquals(3,ageBuckets.getBuckets().size());

    }


}
 
Example 4
Project: googles-monorepo-demo   File: SetGenerators.java   Source Code and License 6 votes vote down vote up
@Override
protected SortedSet<Integer> create(Integer[] elements) {
  SortedSet<Integer> set = nullCheckedTreeSet(elements);
  if (set.isEmpty()) {
    /*
     * The (tooLow + 1, tooHigh) arguments below would be invalid because tooLow would be
     * greater than tooHigh.
     */
    return ContiguousSet.create(Range.openClosed(0, 1), DiscreteDomain.integers()).subSet(0, 1);
  }
  int tooHigh = set.last() + 1;
  int tooLow = set.first() - 1;
  set.add(tooHigh);
  set.add(tooLow);
  return checkedCreate(set).subSet(tooLow + 1, tooHigh);
}
 
Example 5
Project: Backmemed   File: BiomeEndDecorator.java   Source Code and License 6 votes vote down vote up
public WorldGenSpikes.EndSpike[] load(Long p_load_1_) throws Exception
{
    List<Integer> list = Lists.newArrayList(ContiguousSet.create(Range.closedOpen(Integer.valueOf(0), Integer.valueOf(10)), DiscreteDomain.integers()));
    Collections.shuffle(list, new Random(p_load_1_.longValue()));
    WorldGenSpikes.EndSpike[] aworldgenspikes$endspike = new WorldGenSpikes.EndSpike[10];

    for (int i = 0; i < 10; ++i)
    {
        int j = (int)(42.0D * Math.cos(2.0D * (-Math.PI + (Math.PI / 10D) * (double)i)));
        int k = (int)(42.0D * Math.sin(2.0D * (-Math.PI + (Math.PI / 10D) * (double)i)));
        int l = ((Integer)list.get(i)).intValue();
        int i1 = 2 + l / 3;
        int j1 = 76 + l * 3;
        boolean flag = l == 1 || l == 2;
        aworldgenspikes$endspike[i] = new WorldGenSpikes.EndSpike(j, k, i1, j1, flag);
    }

    return aworldgenspikes$endspike;
}
 
Example 6
Project: CustomWorldGen   File: BiomeEndDecorator.java   Source Code and License 6 votes vote down vote up
public WorldGenSpikes.EndSpike[] load(Long p_load_1_) throws Exception
{
    List<Integer> list = Lists.newArrayList(ContiguousSet.create(Range.closedOpen(Integer.valueOf(0), Integer.valueOf(10)), DiscreteDomain.integers()));
    Collections.shuffle(list, new Random(p_load_1_.longValue()));
    WorldGenSpikes.EndSpike[] aworldgenspikes$endspike = new WorldGenSpikes.EndSpike[10];

    for (int i = 0; i < 10; ++i)
    {
        int j = (int)(42.0D * Math.cos(2.0D * (-Math.PI + (Math.PI / 10D) * (double)i)));
        int k = (int)(42.0D * Math.sin(2.0D * (-Math.PI + (Math.PI / 10D) * (double)i)));
        int l = ((Integer)list.get(i)).intValue();
        int i1 = 2 + l / 3;
        int j1 = 76 + l * 3;
        boolean flag = l == 1 || l == 2;
        aworldgenspikes$endspike[i] = new WorldGenSpikes.EndSpike(j, k, i1, j1, flag);
    }

    return aworldgenspikes$endspike;
}
 
Example 7
Project: fact-tools   File: Utils.java   Source Code and License 6 votes vote down vote up
/**
 * Return an int array with the ids of the pixelSet belonging to the given Key,
 * return all camera pixel Ids if the set is not existing
 */
public static int[] getValidPixelSetAsIntArr(Data item, int npix, String pixelSetKey) {
    int[] pixels;

    //Load a given pixelset, otherwise use the the whole camera

    if (pixelSetKey == null) {
        ContiguousSet<Integer> numbers = ContiguousSet.create(Range.closed(0, npix - 1), DiscreteDomain.integers());
        pixels = Ints.toArray(numbers);
    } else {
        Utils.isKeyValid(item, pixelSetKey, PixelSet.class);
        PixelSet pixelSet = (PixelSet) item.get(pixelSetKey);
        pixels = pixelSet.toIntArray();
    }

    return pixels;
}
 
Example 8
Project: aet   File: RemoveLinesDataModifier.java   Source Code and License 6 votes vote down vote up
private String modify(String data, Set<Integer> indexesToRemove) {
  List<String> lines = Arrays.asList(StringUtils.split(data, NEWLINE));
  Set<Integer> dataIndexes = ContiguousSet.create(Range.closed(1, lines.size()),
      DiscreteDomain.integers());
  if (!dataIndexes.containsAll(indexesToRemove)) {
    LOGGER.warn("Some of defined ranges exceed source lenght. Source length is: " + lines.size());
  }
  Set<Integer> filtereedIndexesToRemove = Sets.intersection(dataIndexes, indexesToRemove);
  List<String> modifiedLines = new ArrayList<String>(
      lines.size() - filtereedIndexesToRemove.size());
  for (int i = 0; i < lines.size(); i++) {
    if (!filtereedIndexesToRemove.contains(i + 1)) {
      modifiedLines.add(lines.get(i));
    }
  }
  return StringUtils.join(modifiedLines, NEWLINE);
}
 
Example 9
Project: owsi-core-parent   File: AbstractExcelTableExport.java   Source Code and License 6 votes vote down vote up
/**
 * Ajoute les en-têtes dans la feuille de calcul et cache les colonnes qui doivent l'être.
 * 
 * @param sheet feuille de calcul
 * @param rowIndex numéro de la ligne
 * @param columnInfos RangeMap contenant les informations de colonnes (valeurs) et les index sur auxquelles s'appliquent ces colonnes (clés).
 *                    Les "colonnes" s'étendant sur plus d'un index seront automatiquement fusionnées.
 */
protected void addHeadersToSheet(Sheet sheet, int rowIndex, RangeMap<Integer, ColumnInformation> columnInfos) {
	Row rowHeader = sheet.createRow(rowIndex);
	for (Map.Entry<Range<Integer>, ColumnInformation> entry : columnInfos.asMapOfRanges().entrySet()) {
		Range<Integer> range = entry.getKey();
		ColumnInformation columnInformation = entry.getValue();
		
		addHeaderCell(rowHeader, range.lowerEndpoint(), getColumnLabel(columnInformation.getHeaderKey()));
		
		for (Integer columnIndex : ContiguousSet.create(range, DiscreteDomain.integers())) {
			sheet.setColumnHidden(columnIndex, columnInformation.isHidden());
		}
		
		int beginIndex = range.lowerEndpoint();
		int endIndex = range.upperEndpoint();
		if (beginIndex != endIndex) {
			sheet.addMergedRegion(new CellRangeAddress(rowIndex, rowIndex, beginIndex, endIndex));
		}
	}
}
 
Example 10
Project: core-java   File: StreamObserversShould.java   Source Code and License 6 votes vote down vote up
private static void checkOnNext(MemoizingObserver<Object> observer) {
    assertTrue(observer.responses().isEmpty());

    final Object firstResponse = new Object();
    observer.onNext(firstResponse);
    assertEquals(firstResponse, observer.firstResponse());

    final ContiguousSet<Integer> sorted = ContiguousSet.create(Range.closed(1, 20),
                                                               DiscreteDomain.integers());
    final List<Integer> moreResponses = newArrayList(newHashSet(sorted));

    for (Integer element : moreResponses) {
        observer.onNext(element);
    }
    final List<Object> actualResponses = observer.responses();

    assertEquals(firstResponse, actualResponses.get(0));
    assertEquals(moreResponses.size() + 1, // as there was the first response
                 actualResponses.size());
    assertEquals(moreResponses, actualResponses.subList(1, actualResponses.size()));
}
 
Example 11
Project: greyfish   File: Recombinations.java   Source Code and License 6 votes vote down vote up
@VisibleForTesting
UniformCrossover(final RandomGenerator rng, final double p) {
    this(new Function<Integer, Iterable<Integer>>() {
        @Override
        public Iterable<Integer> apply(final Integer bitStringLength) {
            if (bitStringLength * p < 0.01) {
                return Samplings.random(rng).withoutReplacement().sample(
                        ContiguousSet.create(Range.closedOpen(0, bitStringLength), DiscreteDomain.integers()),
                        new BinomialDistribution(rng, bitStringLength, p).sample()
                );
            } else {
                return Iterables.filter(
                        ContiguousSet.create(Range.closedOpen(0, bitStringLength), DiscreteDomain.integers()),
                        new Predicate<Integer>() {
                            @Override
                            public boolean apply(@Nullable final Integer input) {
                                return p > rng.nextFloat();
                            }
                        });
            }
        }
    });
}
 
Example 12
Project: es4sql   File: AggregationTest.java   Source Code and License 6 votes vote down vote up
@Test
public void multipleGroupByTest() throws Exception {
	Set expectedAges = new HashSet<Integer>(ContiguousSet.create(Range.closed(20, 40), DiscreteDomain.integers()));

	Map<String, Set<Integer>> buckets = new HashMap<>();

	Aggregations result = query(String.format("SELECT COUNT(*) FROM %s/account GROUP BY gender, age", TestsConstants.TEST_INDEX));
	Terms gender = result.get("gender");
	for(Terms.Bucket genderBucket : gender.getBuckets()) {
		String genderKey = genderBucket.getKey().toString();
		buckets.put(genderKey, new HashSet<Integer>());
		Terms ageBuckets = (Terms) genderBucket.getAggregations().get("age");
		for(Terms.Bucket ageBucket : ageBuckets.getBuckets()) {
			buckets.get(genderKey).add(Integer.parseInt(ageBucket.getKey().toString()));
		}
	}

	Assert.assertEquals(2, buckets.keySet().size());
	Assert.assertEquals(expectedAges, buckets.get("m"));
	Assert.assertEquals(expectedAges, buckets.get("f"));
}
 
Example 13
Project: guava   File: SetGenerators.java   Source Code and License 6 votes vote down vote up
@Override
protected SortedSet<Integer> create(Integer[] elements) {
  SortedSet<Integer> set = nullCheckedTreeSet(elements);
  if (set.isEmpty()) {
    /*
     * The (tooLow + 1, tooHigh) arguments below would be invalid because tooLow would be
     * greater than tooHigh.
     */
    return ContiguousSet.create(Range.openClosed(0, 1), DiscreteDomain.integers()).subSet(0, 1);
  }
  int tooHigh = set.last() + 1;
  int tooLow = set.first() - 1;
  set.add(tooHigh);
  set.add(tooLow);
  return checkedCreate(set).subSet(tooLow + 1, tooHigh);
}
 
Example 14
Project: guava   File: SetGenerators.java   Source Code and License 6 votes vote down vote up
@Override
protected SortedSet<Integer> create(Integer[] elements) {
  SortedSet<Integer> set = nullCheckedTreeSet(elements);
  if (set.isEmpty()) {
    /*
     * The (tooLow + 1, tooHigh) arguments below would be invalid because tooLow would be
     * greater than tooHigh.
     */
    return ContiguousSet.create(Range.openClosed(0, 1), DiscreteDomain.integers()).subSet(0, 1);
  }
  int tooHigh = set.last() + 1;
  int tooLow = set.first() - 1;
  set.add(tooHigh);
  set.add(tooLow);
  return checkedCreate(set).subSet(tooLow + 1, tooHigh);
}
 
Example 15
Project: elasticsearch-sql   File: AggregationTest.java   Source Code and License 6 votes vote down vote up
@Test
public void multipleGroupByTest() throws Exception {
	Set expectedAges = new HashSet<Integer>(ContiguousSet.create(Range.closed(20, 40), DiscreteDomain.integers()));

	Map<String, Set<Integer>> buckets = new HashMap<>();

	Aggregations result = query(String.format("SELECT COUNT(*) FROM %s/account GROUP BY gender,  terms('field'='age','size'=200,'alias'='age')", TEST_INDEX_ACCOUNT));
	Terms gender = result.get("gender");
	for(Terms.Bucket genderBucket : gender.getBuckets()) {
		String genderKey = genderBucket.getKey().toString();
		buckets.put(genderKey, new HashSet<Integer>());
		Terms ageBuckets = (Terms) genderBucket.getAggregations().get("age");
		for(Terms.Bucket ageBucket : ageBuckets.getBuckets()) {
			buckets.get(genderKey).add(Integer.parseInt(ageBucket.getKey().toString()));
		}
	}

	Assert.assertEquals(2, buckets.keySet().size());
	Assert.assertEquals(expectedAges, buckets.get("m"));
	Assert.assertEquals(expectedAges, buckets.get("f"));
}
 
Example 16
Project: elasticsearch-sql   File: AggregationTest.java   Source Code and License 6 votes vote down vote up
@Test
public void multipleGroupBysWithSize() throws Exception {
    Set expectedAges = new HashSet<Integer>(ContiguousSet.create(Range.closed(20, 40), DiscreteDomain.integers()));

    Map<String, Set<Integer>> buckets = new HashMap<>();

    Aggregations result = query(String.format("SELECT COUNT(*) FROM %s/account GROUP BY gender, terms('alias'='ageAgg','field'='age','size'=3)", TEST_INDEX_ACCOUNT));
    Terms gender = result.get("gender");
    Assert.assertEquals(2,gender.getBuckets().size());
    for(Terms.Bucket genderBucket : gender.getBuckets()) {

        String genderKey = genderBucket.getKey().toString();
        buckets.put(genderKey, new HashSet<Integer>());
        Terms ageBuckets = genderBucket.getAggregations().get("ageAgg");
        Assert.assertEquals(3,ageBuckets.getBuckets().size());

    }


}
 
Example 17
Project: java-util-examples   File: GenerateOddNumbers.java   Source Code and License 6 votes vote down vote up
@Test
public void generate_odd_numbers_in_range_guava() {

	Set<Integer> set = ContiguousSet.create(Range.closed(1, 10),
			DiscreteDomain.integers());

	Iterable<Integer> oddNumbers = Iterables.filter(set,
			new Predicate<Integer>() {
				@Override
				public boolean apply(Integer input) {
					return input % 2 != 0;
				}
			});

	logger.info(oddNumbers);

	assertThat(
			oddNumbers,
			contains(new Integer(1), new Integer(3), new Integer(5),
					new Integer(7), new Integer(9)));
}
 
Example 18
Project: java-util-examples   File: GenerateEvenNumbers.java   Source Code and License 6 votes vote down vote up
@Test
public void generate_even_numbers_in_range_guava() {

	Set<Integer> set = ContiguousSet.create(Range.closed(1, 10),
			DiscreteDomain.integers());

	Iterable<Integer> evenNumbers = Iterables.filter(set,
			new Predicate<Integer>() {
				@Override
				public boolean apply(Integer input) {
					return input % 2 == 0;
				}
			});


	assertThat(
			evenNumbers,
			contains(new Integer(2), new Integer(4), new Integer(6),
					new Integer(8), new Integer(10)));
}
 
Example 19
Project: SpigotSource   File: BiomeTheEndDecorator.java   Source Code and License 6 votes vote down vote up
public WorldGenEnder.Spike[] a(Long olong) throws Exception {
    ArrayList arraylist = Lists.newArrayList(ContiguousSet.create(Range.closedOpen(Integer.valueOf(0), Integer.valueOf(10)), DiscreteDomain.integers()));

    Collections.shuffle(arraylist, new Random(olong.longValue()));
    WorldGenEnder.Spike[] aworldgenender_spike = new WorldGenEnder.Spike[10];

    for (int i = 0; i < 10; ++i) {
        int j = (int) (42.0D * Math.cos(2.0D * (-3.141592653589793D + 0.3141592653589793D * (double) i)));
        int k = (int) (42.0D * Math.sin(2.0D * (-3.141592653589793D + 0.3141592653589793D * (double) i)));
        int l = ((Integer) arraylist.get(i)).intValue();
        int i1 = 2 + l / 3;
        int j1 = 76 + l * 3;
        boolean flag = l == 1 || l == 2;

        aworldgenender_spike[i] = new WorldGenEnder.Spike(j, k, i1, j1, flag);
    }

    return aworldgenender_spike;
}
 
Example 20
Project: MFIBlocking   File: MathUtilsTest.java   Source Code and License 6 votes vote down vote up
@Test
public void testNormilize() throws Exception {
    int[] rangeNumbersPrimitive = Ints.toArray(ContiguousSet.create(Range.closed(1, 10), DiscreteDomain.integers()));
    HashMap<Integer, Float> idToValue = new HashMap<>();
    for (int index = 0; index < rangeNumbersPrimitive.length; index++) {
        idToValue.put(index, (float) rangeNumbersPrimitive[index]);
    }
    HashMap<Integer, Float> copyMap = new HashMap<>(idToValue);
    MathUtils.normilize(idToValue);

    //assert range
    for (Float normValue : idToValue.values()) {
        assertThat((double) normValue, allOf(lessThanOrEqualTo(1.0), greaterThanOrEqualTo(0.0)));
    }
    //assert values
    for (Map.Entry<Integer, Float> entry : idToValue.entrySet()) {
        Integer key = entry.getKey();
        assertThat((double) entry.getValue(), closeTo((copyMap.get(key) - 1.0) / (9), 0.0001));
    }
}
 
Example 21
Project: levelup-java-examples   File: GenerateOddNumbers.java   Source Code and License 6 votes vote down vote up
@Test
public void generate_odd_numbers_in_range_guava() {

	Set<Integer> set = ContiguousSet.create(Range.closed(1, 10),
			DiscreteDomain.integers());

	Iterable<Integer> oddNumbers = Iterables.filter(set,
			new Predicate<Integer>() {
				@Override
				public boolean apply(Integer input) {
					return input % 2 != 0;
				}
			});

	logger.info(oddNumbers);

	assertThat(
			oddNumbers,
			contains(new Integer(1), new Integer(3), new Integer(5),
					new Integer(7), new Integer(9)));
}
 
Example 22
Project: levelup-java-examples   File: GenerateEvenNumbers.java   Source Code and License 6 votes vote down vote up
@Test
public void generate_even_numbers_in_range_guava() {

	Set<Integer> set = ContiguousSet.create(Range.closed(1, 10),
			DiscreteDomain.integers());

	Iterable<Integer> evenNumbers = Iterables.filter(set,
			new Predicate<Integer>() {
				@Override
				public boolean apply(Integer input) {
					return input % 2 == 0;
				}
			});


	assertThat(
			evenNumbers,
			contains(new Integer(2), new Integer(4), new Integer(6),
					new Integer(8), new Integer(10)));
}
 
Example 23
Project: sdf   File: CharacterClassRangeSet.java   Source Code and License 6 votes vote down vote up
private final BitSet convertToBitSet(final RangeSet<Integer> rangeSet) {
    if(rangeSet.isEmpty()) {
        return new BitSet();
    }

    final BitSet bitSet = new BitSet(rangeSet.span().upperEndpoint());

    rangeSet.asRanges().forEach(range -> {
        if(!range.canonical(DiscreteDomain.integers()).isEmpty()) {
            Range<Integer> closedRange = ContiguousSet.create(range, DiscreteDomain.integers()).range();
            bitSet.set(closedRange.lowerEndpoint(), closedRange.upperEndpoint() + 1);
        }
    });

    return bitSet;
}
 
Example 24
Project: sdf   File: CharacterClassRangeSet.java   Source Code and License 6 votes vote down vote up
@Override public String toString() {
    final List<String> ranges = new ArrayList<>();

    rangeSet.asRanges().forEach(range -> {
        if(!range.isEmpty()) {
            Range<Integer> closedRange = ContiguousSet.create(range, DiscreteDomain.integers()).range();
            final int from = closedRange.lowerEndpoint();
            final int to = closedRange.upperEndpoint();

            if(from != to)
                ranges.add("" + from + "-" + to);
            else
                ranges.add("" + from);
        }
    });

    if(containsEOF)
        ranges.add("EOF");

    return "[" + String.join(",", ranges) + "]";
}
 
Example 25
Project: RoaringBitmap   File: TestRoaringBitmap.java   Source Code and License 6 votes vote down vote up
@Test
public void testFirstLast_AfterLazyMutation2() {
  RoaringBitmap rb = new RoaringBitmap();
  Iterable<Integer> willForceUseOfBitmapContainer = Iterables.filter(
          ContiguousSet.create(Range.openClosed(0, 1 << 16), DiscreteDomain.integers()),
          new Predicate<Integer>() {
            @Override
            public boolean apply(Integer input) {
              return input % 3 == 0;
            }
          }
          );
  int max = 0;
  for(Integer i : willForceUseOfBitmapContainer) {
    rb.add(i);
    max = i;
  }
  Assert.assertEquals(3, rb.first());
  Assert.assertEquals(max, rb.last());
  RoaringBitmap mutator = new RoaringBitmap();
  mutator.add(0, 2, 4, 6, 8);
  rb.lazyor(mutator);
  Assert.assertEquals(0, rb.first());
  Assert.assertEquals(max, rb.last());
}
 
Example 26
Project: pentaho-kettle   File: MemoryGroupByAggregationTest.java   Source Code and License 6 votes vote down vote up
private Iterable<Object[]> getRows() {
  if ( data.isEmpty() ) {
    return ImmutableSet.of();
  }

  Range<Integer> rows = Range.closed( 0, data.rowMap().lastKey() );

  return FluentIterable.from( ContiguousSet.create( rows, DiscreteDomain.integers() ) )
    .transform( Functions.forMap( data.rowMap(), ImmutableMap.<Integer, Optional<Object>>of() ) )
    .transform( new Function<Map<Integer, Optional<Object>>, Object[]>() {
      @Override public Object[] apply( Map<Integer, Optional<Object>> input ) {
        Object[] row = new Object[rowMeta.size()];
        for ( Map.Entry<Integer, Optional<Object>> entry : input.entrySet() ) {
          row[entry.getKey()] = entry.getValue().orNull();
        }
        return row;
      }
    } );
}
 
Example 27
Project: BlackBananaCoin   File: Foo.java   Source Code and License 6 votes vote down vote up
public static void main(String[] args) {
	// why isn't the maximum target 2^256 - 1
	BigInteger unitTestTarget = BigInteger.valueOf (1).shiftLeft (256).subtract (BigInteger.ONE);
	System.out.println(unitTestTarget);
	System.out.println(unitTestTarget.toString(16));

	BigInteger testnet3Target = BigInteger.valueOf (0xFFFFL).shiftLeft (8 * (0x1d - 3));
	System.out.println(testnet3Target);
	System.out.println(testnet3Target.toString(16));
	
	ContiguousSet<Integer> set = ContiguousSet.create(Range.closed(Integer.MIN_VALUE, Integer.MAX_VALUE), integers());
	System.out.println(new Date());
	int count = 0;
	for(Integer v : set){
		count++;
	}
	System.out.println(new Date());		
	
}
 
Example 28
Project: open-kilda   File: ResourcePool.java   Source Code and License 5 votes vote down vote up
/**
 * Allocates resource id.
 *
 * @return allocated resource id
 */
public Integer allocate() {
    for (Integer id : ContiguousSet.create(range, DiscreteDomain.integers())) {
        if (resources.add(id)) {
            return id;
        }
    }
    throw new ArrayIndexOutOfBoundsException("Could not allocate resource: pool is full");
}
 
Example 29
Project: guava-mock   File: SetGenerators.java   Source Code and License 5 votes vote down vote up
protected final ContiguousSet<Integer> checkedCreate(SortedSet<Integer> elementsSet) {
  List<Integer> elements = newArrayList(elementsSet);
  /*
   * A ContiguousSet can't have holes. If a test demands a hole, it should be changed so that it
   * doesn't need one, or it should be suppressed for ContiguousSet.
   */
  for (int i = 0; i < elements.size() - 1; i++) {
    assertEquals(elements.get(i) + 1, (int) elements.get(i + 1));
  }
  Range<Integer> range =
      (elements.isEmpty()) ? Range.closedOpen(0, 0) : Range.encloseAll(elements);
  return ContiguousSet.create(range, DiscreteDomain.integers());
}
 
Example 30
Project: matplotlib4j   File: NumpyUtils.java   Source Code and License 5 votes vote down vote up
public static List<Double> arange(double start, double end, double step) {
    double scaledStart = start / step;
    double scaledEnd = end / step;
    double floorGap = scaledStart - (int) scaledStart;
    return ContiguousSet.create(Range.closed((int) scaledStart, (int) scaledEnd), DiscreteDomain.integers())
            .stream().map(x -> (x + floorGap) * step).collect(Collectors.toList());
}
 
Example 31
Project: es-sql   File: AggregationTest.java   Source Code and License 5 votes vote down vote up
@Test
public void testSubAggregations() throws  Exception {
	Set expectedAges = new HashSet<>(ContiguousSet.create(Range.closed(20, 40), DiscreteDomain.integers()));
	final String query = String.format("SELECT /*! DOCS_WITH_AGGREGATION(10) */" +
               " * FROM %s/account GROUP BY (gender, terms('field'='age','size'=200,'alias'='age')), (state) LIMIT 200,200", TEST_INDEX);

	Map<String, Set<Integer>> buckets = new HashMap<>();

       SqlElasticSearchRequestBuilder select = getSearchRequestBuilder(query);
	SearchResponse response = (SearchResponse) select.get();
	Aggregations result = response.getAggregations();

	Terms gender = result.get("gender");
	for(Terms.Bucket genderBucket : gender.getBuckets()) {
		String genderKey = genderBucket.getKey().toString();
		buckets.put(genderKey, new HashSet<Integer>());
		Terms ageBuckets = (Terms) genderBucket.getAggregations().get("age");
		for(Terms.Bucket ageBucket : ageBuckets.getBuckets()) {
			buckets.get(genderKey).add(Integer.parseInt(ageBucket.getKey().toString()));
		}
	}

	Assert.assertEquals(2, buckets.keySet().size());
	Assert.assertEquals(expectedAges, buckets.get("m"));
	Assert.assertEquals(expectedAges, buckets.get("f"));

	Terms state = result.get("state");
	for(Terms.Bucket stateBucket : state.getBuckets()) {
		if(stateBucket.getKey().toString().equalsIgnoreCase("ak")) {
			Assert.assertTrue("There are 22 entries for state ak", stateBucket.getDocCount() == 22);
		}
	}

	Assert.assertEquals(response.getHits().totalHits(), 1000);
	Assert.assertEquals(response.getHits().hits().length, 10);
}
 
Example 32
Project: googles-monorepo-demo   File: SetGenerators.java   Source Code and License 5 votes vote down vote up
protected final ContiguousSet<Integer> checkedCreate(SortedSet<Integer> elementsSet) {
  List<Integer> elements = newArrayList(elementsSet);
  /*
   * A ContiguousSet can't have holes. If a test demands a hole, it should be changed so that it
   * doesn't need one, or it should be suppressed for ContiguousSet.
   */
  for (int i = 0; i < elements.size() - 1; i++) {
    assertEquals(elements.get(i) + 1, (int) elements.get(i + 1));
  }
  Range<Integer> range =
      (elements.isEmpty()) ? Range.closedOpen(0, 0) : Range.encloseAll(elements);
  return ContiguousSet.create(range, DiscreteDomain.integers());
}
 
Example 33
Project: Mastering-Mesos   File: JobDiff.java   Source Code and License 5 votes vote down vote up
/**
 * Creates a map of {@code instanceCount} copies of {@code config}.
 *
 * @param config Configuration to generate an instance mapping for.
 * @param instanceCount Number of instances to represent.
 * @return A map of instance IDs (from 0 to {@code instanceCount - 1}) to {@code config}.
 */
public static Map<Integer, ITaskConfig> asMap(ITaskConfig config, int instanceCount) {
  requireNonNull(config);

  Set<Integer> desiredInstances = ContiguousSet.create(
      Range.closedOpen(0, instanceCount),
      DiscreteDomain.integers());
  return ImmutableMap.copyOf(Maps.asMap(desiredInstances, Functions.constant(config)));
}
 
Example 34
Project: Mastering-Mesos   File: SanitizedConfiguration.java   Source Code and License 5 votes vote down vote up
/**
 * Constructs a SanitizedConfiguration object and populates the set of instance IDs for
 * the provided {@link org.apache.aurora.scheduler.storage.entities.ITaskConfig}.
 *
 * @param sanitized A sanitized configuration.
 */
@VisibleForTesting
public SanitizedConfiguration(IJobConfiguration sanitized) {
  this.sanitized = sanitized;
  this.instanceIds = ContiguousSet.create(
      Range.closedOpen(0, sanitized.getInstanceCount()),
      DiscreteDomain.integers());
}
 
Example 35
Project: Mastering-Mesos   File: CrontabEntry.java   Source Code and License 5 votes vote down vote up
private String fieldToString(RangeSet<Integer> rangeSet, Range<Integer> coveringRange) {
  if (rangeSet.asRanges().size() == 1 && rangeSet.encloses(coveringRange)) {
    return "*";
  }
  List<String> components = Lists.newArrayList();
  for (Range<Integer> range : rangeSet.asRanges()) {
    ContiguousSet<Integer> set = ContiguousSet.create(range, DiscreteDomain.integers());
    if (set.size() == 1) {
      components.add(set.first().toString());
    } else {
      components.add(set.first() + "-" + set.last());
    }
  }
  return String.join(",", components);
}
 
Example 36
Project: grakn   File: FibonacciQueueTest.java   Source Code and License 5 votes vote down vote up
@Test
public void testIterator() {
    // insert lots of numbers in order
    final Set<Integer> values = ContiguousSet.create(closedOpen(0, 1000), integers());
    final FibonacciQueue<Integer> queue = FibonacciQueue.create();
    assertTrue(queue.addAll(values));
    assertEquals(values, ImmutableSet.copyOf(queue.iterator()));
    assertEquals(values, ImmutableSet.copyOf(queue));
}
 
Example 37
Project: beam   File: ReadUnboundTranslatorTest.java   Source Code and License 5 votes vote down vote up
@Test
public void testReadBounded() throws Exception {
  ApexPipelineOptions options = PipelineOptionsFactory.create()
      .as(ApexPipelineOptions.class);
  EmbeddedCollector.RESULTS.clear();
  options.setApplicationName("ReadBounded");
  options.setRunner(ApexRunner.class);
  Pipeline p = Pipeline.create(options);

  Set<Long> expected = ContiguousSet.create(Range.closedOpen(0L, 10L), DiscreteDomain.longs());
  p.apply(GenerateSequence.from(0).to(10))
      .apply(ParDo.of(new EmbeddedCollector()));

  ApexRunnerResult result = (ApexRunnerResult) p.run();
  DAG dag = result.getApexDAG();
  String operatorName = "GenerateSequence/Read(BoundedCountingSource)";
  DAG.OperatorMeta om = dag.getOperatorMeta(operatorName);
  Assert.assertNotNull(om);
  Assert.assertEquals(om.getOperator().getClass(), ApexReadUnboundedInputOperator.class);

  long timeout = System.currentTimeMillis() + 30000;
  while (System.currentTimeMillis() < timeout) {
    if (EmbeddedCollector.RESULTS.containsAll(expected)) {
      break;
    }
    LOG.info("Waiting for expected results.");
    Thread.sleep(1000);
  }
  Assert.assertEquals(Sets.newHashSet(expected), EmbeddedCollector.RESULTS);
}
 
Example 38
Project: beam   File: UnboundedReadEvaluatorFactoryTest.java   Source Code and License 5 votes vote down vote up
@Test
public void generatesInitialSplits() throws Exception {
  when(context.createRootBundle()).thenAnswer(new Answer<UncommittedBundle<?>>() {
    @Override
    public UncommittedBundle<?> answer(InvocationOnMock invocation) throws Throwable {
      return bundleFactory.createRootBundle();
    }
  });

  int numSplits = 5;
  Collection<CommittedBundle<?>> initialInputs =
      new UnboundedReadEvaluatorFactory.InputProvider(context)
          .getInitialInputs(graph.getProducer(longs), numSplits);
  // CountingSource.unbounded has very good splitting behavior
  assertThat(initialInputs, hasSize(numSplits));

  int readPerSplit = 100;
  int totalSize = numSplits * readPerSplit;
  Set<Long> expectedOutputs =
      ContiguousSet.create(Range.closedOpen(0L, (long) totalSize), DiscreteDomain.longs());

  Collection<Long> readItems = new ArrayList<>(totalSize);
  for (CommittedBundle<?> initialInput : initialInputs) {
    CommittedBundle<UnboundedSourceShard<Long, ?>> shardBundle =
        (CommittedBundle<UnboundedSourceShard<Long, ?>>) initialInput;
    WindowedValue<UnboundedSourceShard<Long, ?>> shard =
        Iterables.getOnlyElement(shardBundle.getElements());
    assertThat(shard.getTimestamp(), equalTo(BoundedWindow.TIMESTAMP_MIN_VALUE));
    assertThat(shard.getWindows(), Matchers.<BoundedWindow>contains(GlobalWindow.INSTANCE));
    UnboundedSource<Long, ?> shardSource = shard.getValue().getSource();
    readItems.addAll(
        SourceTestUtils.readNItemsFromUnstartedReader(
            shardSource.createReader(
                PipelineOptionsFactory.create(), null /* No starting checkpoint */),
            readPerSplit));
  }
  assertThat(readItems, containsInAnyOrder(expectedOutputs.toArray(new Long[0])));
}
 
Example 39
Project: beam   File: UnboundedReadEvaluatorFactoryTest.java   Source Code and License 5 votes vote down vote up
@Test
public void evaluatorThrowsInCloseRethrows() throws Exception {
  ContiguousSet<Long> elems = ContiguousSet.create(Range.closed(0L, 20L), DiscreteDomain.longs());
  TestUnboundedSource<Long> source =
      new TestUnboundedSource<>(BigEndianLongCoder.of(), elems.toArray(new Long[0]))
          .throwsOnClose();

  PCollection<Long> pcollection = p.apply(Read.from(source));
  AppliedPTransform<?, ?, ?> sourceTransform =
      DirectGraphs.getGraph(p).getProducer(pcollection);

  when(context.createRootBundle()).thenReturn(bundleFactory.createRootBundle());
  UncommittedBundle<Long> output = bundleFactory.createBundle(pcollection);
  when(context.createBundle(pcollection)).thenReturn(output);

  WindowedValue<UnboundedSourceShard<Long, TestCheckpointMark>> shard =
      WindowedValue.valueInGlobalWindow(
          UnboundedSourceShard.unstarted(source, NeverDeduplicator.create()));
  CommittedBundle<UnboundedSourceShard<Long, TestCheckpointMark>> inputBundle =
      bundleFactory
          .<UnboundedSourceShard<Long, TestCheckpointMark>>createRootBundle()
          .add(shard)
          .commit(Instant.now());
  UnboundedReadEvaluatorFactory factory =
      new UnboundedReadEvaluatorFactory(context, 0.0 /* never reuse */);
  TransformEvaluator<UnboundedSourceShard<Long, TestCheckpointMark>> evaluator =
      factory.forApplication(sourceTransform, inputBundle);
  thrown.expect(IOException.class);
  thrown.expectMessage("throws on close");
  evaluator.processElement(shard);
}
 
Example 40
Project: nomulus   File: TimeOfYear.java   Source Code and License 5 votes vote down vote up
/**
 * Returns an {@link Iterable} of {@link DateTime}s of every recurrence of this particular
 * time of year within a given {@link Range} (usually one spanning many years).
 *
 * <p>WARNING: This can return a potentially very large {@link Iterable} if {@code END_OF_TIME}
 * is used as the upper endpoint of the range.
 */
public Iterable<DateTime> getInstancesInRange(Range<DateTime> range) {
  // In registry world, all dates are within START_OF_TIME and END_OF_TIME, so restrict any
  // ranges without bounds to our notion of zero-to-infinity.
  Range<DateTime> normalizedRange = range.intersection(Range.closed(START_OF_TIME, END_OF_TIME));
  Range<Integer> yearRange = Range.closed(
      normalizedRange.lowerEndpoint().getYear(),
      normalizedRange.upperEndpoint().getYear());
  return ContiguousSet.create(yearRange, integers())
      .stream()
      .map(this::getDateTimeWithYear)
      .filter(normalizedRange)
      .collect(toImmutableList());
}
 
Example 41
Project: Camel   File: IgniteMessagingTest.java   Source Code and License 5 votes vote down vote up
@Test
public void testProducerSendManyMessages() {
    List<Object> messages = Lists.newArrayList();
    setupMessageListener(TOPIC1, messages);

    Set<Integer> request = ContiguousSet.create(Range.closedOpen(0, 100), DiscreteDomain.integers());
    template.requestBody("ignite:messaging:TOPIC1", request);

    await().atMost(5, TimeUnit.SECONDS).untilCall(to(messages).size(), equalTo(100));
    assert_().that(messages).containsAllIn(request);
}
 
Example 42
Project: Camel   File: IgniteMessagingTest.java   Source Code and License 5 votes vote down vote up
@Test
public void testProducerSendManyMessagesOrdered() {
    List<Object> messages = Lists.newArrayList();
    setupMessageListener(TOPIC1, messages);

    ContiguousSet<Integer> set = ContiguousSet.create(Range.closedOpen(0, 100), DiscreteDomain.integers());
    for (int i : set) {
        template.requestBody("ignite:messaging:TOPIC1?sendMode=ORDERED&timeout=1000", i);
    }

    await().atMost(5, TimeUnit.SECONDS).untilCall(to(messages).size(), equalTo(100));
    assert_().that(messages).containsAllIn(set);
}
 
Example 43
Project: Camel   File: IgniteMessagingTest.java   Source Code and License 5 votes vote down vote up
@Test
public void testProducerSendCollectionAsObject() {
    List<Object> messages = Lists.newArrayList();
    setupMessageListener(TOPIC1, messages);

    Set<Integer> request = ContiguousSet.create(Range.closedOpen(0, 100), DiscreteDomain.integers());
    template.requestBody("ignite:messaging:TOPIC1?treatCollectionsAsCacheObjects=true", request);

    await().atMost(5, TimeUnit.SECONDS).untilCall(to(messages).size(), equalTo(1));
    assert_().that(messages.get(0)).isEqualTo(request);
}
 
Example 44
Project: Camel   File: IgniteMessagingTest.java   Source Code and License 5 votes vote down vote up
@Test
public void testConsumerManyMessages() throws Exception {
    List<Object> messages = Lists.newArrayList();
    Consumer consumer = context.getEndpoint("ignite:messaging:TOPIC1").createConsumer(storeBodyInListProcessor(messages));
    consumer.start();

    Set<Integer> messagesToSend = ContiguousSet.create(Range.closedOpen(0, 100), DiscreteDomain.integers());
    ignite().message().send(TOPIC1, messagesToSend);

    await().atMost(5, TimeUnit.SECONDS).untilCall(to(messages).size(), equalTo(100));

    consumer.stop();
}
 
Example 45
Project: FinanceAnalytics   File: SimpleResultBuilder.java   Source Code and License 5 votes vote down vote up
/**
 * Builds a {@link ScenarioResultModel} from the data calculated in a single cycle.
 * @param resultModel the results calculated by the engine in a single calculation cycle
 * @param columnNames column name overrides
 * @return A simple result model built from the results
 * @throws IllegalArgumentException if the number of column names doesn't match the number of columns
 */
public SimpleResultModel build(ViewResultModel resultModel, List<String> columnNames) {
  ArgumentChecker.notNull(columnNames, "columnNames");
  ArgumentChecker.notNull(resultModel, "resultModel");

  if (columnNames.size() != _columnNames.size()) {
    throw new IllegalArgumentException("Wrong number of column names. expected: " + _columnNames.size() +
                                           ", actual: " + columnNames.size());
  }
  int rowCount = _idToIndex.size();
  int colCount = columnNames.size();
  ContiguousSet<Integer> rowIndices = ContiguousSet.create(Range.closedOpen(0, rowCount), DiscreteDomain.integers());
  ContiguousSet<Integer> colIndices = ContiguousSet.create(Range.closedOpen(0, colCount), DiscreteDomain.integers());
  Table<Integer, Integer, Object> table = ArrayTable.create(rowIndices, colIndices);

  for (ViewResultEntry entry : resultModel.getAllResults()) {
    String calcConfigName = entry.getCalculationConfiguration();
    ComputedValueResult value = entry.getComputedValue();
    ValueSpecification valueSpec = value.getSpecification();
    CompiledViewCalculationConfiguration calcConfig = _compiledViewDef.getCompiledCalculationConfiguration(calcConfigName);
    Set<ValueRequirement> valueReqs = calcConfig.getTerminalOutputSpecifications().get(valueSpec);

    for (ValueRequirement valueReq : valueReqs) {
      ColumnSpec colSpec = new ColumnSpec(calcConfigName, valueReq.getValueName(), valueReq.getConstraints());
      Integer colIndex = _colToIndex.get(colSpec);
      Integer rowIndex = _idToIndex.get(valueReq.getTargetReference().getSpecification().getUniqueId().getObjectId());

      // there won't be a row or column index for specific outputs (e.g. curves)
      if (colIndex != null && rowIndex != null) {
        table.put(rowIndex, colIndex, value);
      }
      // TODO handle specific outputs
    }
  }
  return new SimpleResultModel(_targets, columnNames, table, resultModel.getViewCycleExecutionOptions());
}
 
Example 46
Project: owsi-core-parent   File: NotificationDemoEntry.java   Source Code and License 5 votes vote down vote up
protected final <E extends GenericEntity<Long, ?>> E getFirstInRange(Class<E> clazz, Range<Long> range, Predicate<E> predicate) {
	E entity = getFirstWithId(clazz, ContiguousSet.create(range, DiscreteDomain.longs()), predicate);
	
	if (entity != null) {
		return entity;
	} else {
		LOGGER.error("A demo object is missing for demo " + Classes.simpleName(getClass()));
		Session.get().error(Localizer.get().getString("console.notifications.demo.noDataAvailable", null));
		
		throw ConsoleNotificationDemoIndexPage.linkDescriptor().newRestartResponseException();
	}
}
 
Example 47
Project: owsi-core-parent   File: ContiguousSetModel.java   Source Code and License 5 votes vote down vote up
@Override
public ContiguousSet<C> getObject() {
	Range<C> range = rangeModel.getObject();
	if (range == null) {
		return null;
	} else {
		if (discreteDomain instanceof PartitionDiscreteDomain) {
			range = ((PartitionDiscreteDomain<C>) discreteDomain).alignOut(range);
		}
		return ContiguousSet.create(range, discreteDomain);
	}
}
 
Example 48
Project: owsi-core-parent   File: CollectionDifferIndexStrategy.java   Source Code and License 5 votes vote down vote up
@Override
public Iterable<Integer> difference(Collection<T> source, Collection<T> filter) {
	int sourceSize = source == null ? 0 : Iterables.size(source);
	int filterSize = filter == null ? 0 : Iterables.size(filter);
	if (sourceSize > filterSize) {
		return ContiguousSet.create(Range.closedOpen(filterSize, sourceSize), DiscreteDomain.integers());
	} else {
		return ImmutableList.of();
	}
}
 
Example 49
Project: greyfish   File: Recombinations.java   Source Code and License 5 votes vote down vote up
@VisibleForTesting
NPointCrossover(final int n, final RandomGenerator rng) {
    this(new Function<Integer, Iterable<Integer>>() {
        @Override
        public Iterable<Integer> apply(final Integer bitStringLength) {
            return Samplings.random(rng).withoutReplacement().sample(
                    ContiguousSet.create(Range.closedOpen(0, bitStringLength), DiscreteDomain.integers()), n
            );
        }
    });
}
 
Example 50
Project: todos   File: DigitMappings.java   Source Code and License 5 votes vote down vote up
/**
 * Constructs a {@link DigitMapping} from a {@link RangeSet}, ordering the ranges in ascending
 * order (regardless of input order). If out-of-order ranges are desired, use the
 * {@link #fromRanges(Iterable)} factory method instead to provide explicit ordering.
 */
public static DigitMapping fromRangeSet(RangeSet<Character> spec) {
    return fromRanges(Collections2.transform(spec.asRanges(),
            new Function<Range<Character>, ContiguousSet<Character>>() {
                @Override
                public ContiguousSet<Character> apply(Range<Character> input) {
                    return ContiguousSet.create(input, DOMAIN);
                }
            }));
}
 
Example 51
Project: todos   File: DigitRangeMapping.java   Source Code and License 5 votes vote down vote up
/**
 * Constructs a {@code DigitRangeMapping} from a sequence of ranges (as {@link ContiguousSet}s).
 * Values are assigned to the characters in the order defined by flattening the ranges within
 * the interval. The behavior of this mapping is not defined if the ranges are not disjoint.
 *
 * @param ranges an iterable of disjoint contiguous sets of characters
 */
public DigitRangeMapping(Iterable<ContiguousSet<Character>> ranges) {
    mRanges = ImmutableList.copyOf(ranges);
    int size = 0;
    for (ContiguousSet<Character> range : mRanges) {
        size += range.size();
    }
    mRadix = size;
}
 
Example 52
Project: todos   File: DigitRangeMapping.java   Source Code and License 5 votes vote down vote up
@Override
public char encode(int digit) {
    for (ContiguousSet<Character> range : mRanges) {
        if (digit < range.size()) {
            return range.asList().get(digit);
        }
        digit -= range.size();
    }
    throw new IllegalArgumentException("No encoding for digit " + digit +
            " (radix " + radix() + ")");
}
 
Example 53
Project: todos   File: DigitRangeMapping.java   Source Code and License 5 votes vote down vote up
@Override
public int decode(char encoded) {
    int digit = 0;
    for (ContiguousSet<Character> range : mRanges) {
        if (range.contains(encoded)) {
            return digit + encoded - range.first();
        }
        digit += range.size();
    }
    throw new IllegalArgumentException("Character " + encoded + " is not in digit mapping " +
        this);
}
 
Example 54
Project: typed-github   File: NullabilityTest.java   Source Code and License 5 votes vote down vote up
/**
 * Checks if all params in method have given annotation.
 * @param method Method to be checked
 * @return True if all parameters of method have given annotation
 */
private static boolean allParamsAnnotated(final Method method) {
    return Iterables.all(
        ContiguousSet.create(
            Range.closedOpen(0, method.getParameterTypes().length),
            DiscreteDomain.integers()
        ),
        new Predicate<Integer>() {
            @Override
            public boolean apply(final Integer index) {
                final boolean primitive = method.getParameterTypes()[index]
                    .isPrimitive();
                return primitive || !primitive && Collections2.transform(
                    Arrays.asList(method.getParameterAnnotations()[index]),
                    new Function<Annotation,
                        Class<? extends Annotation>>() {
                        @Override
                        public Class<? extends Annotation> apply(
                            final Annotation input) {
                            return input.annotationType();
                        }
                    }
                ).contains(NotNull.class);
            }
        }
    );
}
 
Example 55
Project: pinlater   File: PinLaterBackendBase.java   Source Code and License 5 votes vote down vote up
public Future<Integer> getJobCount(final PinLaterGetJobCountRequest request) {
  // If no priority is specified, search for jobs of all priorities.
  Range<Integer> priorityRange = request.isSetPriority()
                                 ? Range.closed((int) request.getPriority(),
      (int) request.getPriority()) :
                                 Range.closed(1, numPriorityLevels);
  final ContiguousSet<Integer> priorities =
      ContiguousSet.create(priorityRange, DiscreteDomain.integers());

  // Execute count query on each shard in parallel.
  List<Future<Integer>> futures = Lists.newArrayListWithCapacity(getShards().size());
  for (final String shardName : getShards()) {
    futures.add(futurePool.apply(new ExceptionalFunction0<Integer>() {
      @Override
      public Integer applyE() throws Throwable {
        return getJobCountFromShard(
            request.getQueueName(),
            shardName,
            priorities,
            request.getJobState(),
            request.isCountFutureJobs(),
            request.getBodyRegexToMatch());
      }
    }));
  }

  return Future.collect(futures).map(
      new Function<List<Integer>, Integer>() {
        @Override
        public Integer apply(List<Integer> shardCounts) {
          int totalCount = 0;
          for (Integer shardCount : shardCounts) {
            totalCount += shardCount;
          }
          return totalCount;
        }
      });
}
 
Example 56
Project: guava-libraries   File: SetGenerators.java   Source Code and License 5 votes vote down vote up
@Override protected SortedSet<Integer> create(Integer[] elements) {
  SortedSet<Integer> set = nullCheckedTreeSet(elements);
  if (set.isEmpty()) {
    /*
     * The (tooLow + 1, tooHigh) arguments below would be invalid because tooLow would be
     * greater than tooHigh.
     */
    return ContiguousSet.create(Range.openClosed(0, 1), DiscreteDomain.integers()).subSet(0, 1);
  }
  int tooHigh = set.last() + 1;
  int tooLow = set.first() - 1;
  set.add(tooHigh);
  set.add(tooLow);
  return checkedCreate(set).subSet(tooLow + 1, tooHigh);
}
 
Example 57
Project: guava-libraries   File: SetGenerators.java   Source Code and License 5 votes vote down vote up
protected final ContiguousSet<Integer> checkedCreate(SortedSet<Integer> elementsSet) {
  List<Integer> elements = newArrayList(elementsSet);
  /*
   * A ContiguousSet can't have holes. If a test demands a hole, it should be changed so that it
   * doesn't need one, or it should be suppressed for ContiguousSet.
   */
  for (int i = 0; i < elements.size() - 1; i++) {
    assertEquals(elements.get(i) + 1, (int) elements.get(i + 1));
  }
  Range<Integer> range =
      (elements.isEmpty()) ? Range.closedOpen(0, 0) : Range.encloseAll(elements);
  return ContiguousSet.create(range, DiscreteDomain.integers());
}
 
Example 58
Project: guava-libraries   File: SetGenerators.java   Source Code and License 5 votes vote down vote up
@Override protected SortedSet<Integer> create(Integer[] elements) {
  SortedSet<Integer> set = nullCheckedTreeSet(elements);
  if (set.isEmpty()) {
    /*
     * The (tooLow + 1, tooHigh) arguments below would be invalid because tooLow would be
     * greater than tooHigh.
     */
    return ContiguousSet.create(Range.openClosed(0, 1), DiscreteDomain.integers()).subSet(0, 1);
  }
  int tooHigh = set.last() + 1;
  int tooLow = set.first() - 1;
  set.add(tooHigh);
  set.add(tooLow);
  return checkedCreate(set).subSet(tooLow + 1, tooHigh);
}
 
Example 59
Project: guava-libraries   File: SetGenerators.java   Source Code and License 5 votes vote down vote up
protected final ContiguousSet<Integer> checkedCreate(SortedSet<Integer> elementsSet) {
  List<Integer> elements = newArrayList(elementsSet);
  /*
   * A ContiguousSet can't have holes. If a test demands a hole, it should be changed so that it
   * doesn't need one, or it should be suppressed for ContiguousSet.
   */
  for (int i = 0; i < elements.size() - 1; i++) {
    assertEquals(elements.get(i) + 1, (int) elements.get(i + 1));
  }
  Range<Integer> range =
      (elements.isEmpty()) ? Range.closedOpen(0, 0) : Range.encloseAll(elements);
  return ContiguousSet.create(range, DiscreteDomain.integers());
}
 
Example 60
Project: GeneralUtils   File: GuavaSetsTest.java   Source Code and License 5 votes vote down vote up
@Test
public void whenCreatingRangeOfIntegersSet_thenCreated() {
    int start = 10;
    int end = 30;
    ContiguousSet<Integer> set = ContiguousSet.create(
      Range.closed(start, end), DiscreteDomain.integers());
 
    assertEquals(21, set.size());
    assertEquals(10, set.first().intValue());
    assertEquals(30, set.last().intValue());
}