@Override
  public Iterator<? extends T> iterator(long first, long count) {
    Collection<T> data = dataModel.getObject();
    if (data == null || data.size() == 0) return Iterators.emptyIterator();
    Iterator<T> it;
    final SortParam<S> sortParam = getSort();
    if (sortParam != null && sortParam.getProperty() != null) {
      Ordering<T> ordering =
          Ordering.natural()
              .nullsFirst()
              .onResultOf(
                  new Function<T, Comparable<?>>() {

                    @Override
                    public Comparable<?> apply(T input) {
                      return comparableValue(input, sortParam.getProperty());
                    }
                  });
      if (!sortParam.isAscending()) ordering = ordering.reverse();
      it = ordering.sortedCopy(data).iterator();
    } else {
      it = data.iterator();
    }
    if (first > 0) Iterators.advance(it, (int) first);
    return count >= 0 ? Iterators.limit(it, (int) count) : it;
  }
 private String makeSubjectString(
     String httpMethod, String host, String path, final Map<String, List<String>> parameters)
     throws UnsupportedEncodingException {
   parameters.remove("");
   StringBuilder sb = new StringBuilder();
   sb.append(httpMethod);
   sb.append("\n");
   sb.append(host);
   sb.append("\n");
   sb.append(path);
   sb.append("\n");
   String prefix = sb.toString();
   sb = new StringBuilder();
   boolean addedParam = false;
   for (final String paramName : Ordering.natural().sortedCopy(parameters.keySet())) {
     if (SecurityParameter.Signature.parameter().equals(paramName)) continue;
     List<String> paramValues = parameters.get(paramName);
     if (paramValues.isEmpty()) paramValues = Lists.newArrayList("");
     for (final String value : Ordering.natural().sortedCopy(paramValues)) {
       sb.append(urlencode(paramName)).append("=").append(urlencode(value)).append("&");
       addedParam = true;
     }
   }
   if (addedParam) sb.setLength(sb.length() - 1);
   String subject = prefix + sb.toString();
   LOG.trace("VERSION2: " + subject);
   return subject;
 }
  @Test(dependsOnMethods = "testGetLocation")
  public void testResolveVolumeOffering() throws Exception {

    Ordering<StorageOffering> cheapestOrdering =
        new Ordering<StorageOffering>() {
          public int compare(StorageOffering left, StorageOffering right) {
            return ComparisonChain.start()
                .compare(left.getPrice().getRate(), right.getPrice().getRate())
                .result();
          }
        }.reverse();

    Iterable<? extends StorageOffering> storageOfferingsThatAreInOurLocationAndCorrectFormat =
        filter(
            connection.listStorageOfferings(),
            new Predicate<StorageOffering>() {
              @Override
              public boolean apply(StorageOffering arg0) {

                return arg0.getLocation().equals(location.getId())
                    && Iterables.any(
                        arg0.getFormats(),
                        new Predicate<StorageOffering.Format>() {

                          @Override
                          public boolean apply(Format arg0) {
                            return arg0.getId().equals(FORMAT);
                          }
                        });
              }
            });
    cheapestStorage = cheapestOrdering.max(storageOfferingsThatAreInOurLocationAndCorrectFormat);
    System.out.println(cheapestStorage);
  }
Exemplo n.º 4
0
  /** Constructor */
  public TreeMapStore() {
    /*
     * For the start times index, the "key comparator" will compare the
     * start times as longs directly. This is the primary comparator for its
     * tree map.
     *
     * The secondary "value" comparator will check the end times first, and
     * in the event of a tie, defer to the ISegment's Comparable
     * implementation, a.k.a. its natural ordering.
     *
     * The same is done for the end times index, but swapping the first two
     * comparators instead.
     */
    fStartTimesIndex =
        checkNotNull(
            TreeMultimap.<Long, T>create(
                SegmentComparators.LONG_COMPARATOR,
                Ordering.from(SegmentComparators.INTERVAL_END_COMPARATOR)
                    .compound(Ordering.natural())));

    fEndTimesIndex =
        checkNotNull(
            TreeMultimap.<Long, T>create(
                SegmentComparators.LONG_COMPARATOR,
                Ordering.from(SegmentComparators.INTERVAL_START_COMPARATOR)
                    .compound(Ordering.natural())));

    fSize = 0;
  }
Exemplo n.º 5
0
 @Override
 public ImmutableSortedSet<Path> getSortedMatchingDirectoryContents(
     final Path pathRelativeToProjectRoot, String globPattern) throws IOException {
   Preconditions.checkState(isDirectory(pathRelativeToProjectRoot));
   final PathMatcher pathMatcher = FileSystems.getDefault().getPathMatcher("glob:" + globPattern);
   return FluentIterable.from(fileContents.keySet())
       .filter(
           new Predicate<Path>() {
             @Override
             public boolean apply(Path input) {
               return input.getParent().equals(pathRelativeToProjectRoot)
                   && pathMatcher.matches(input.getFileName());
             }
           })
       .toSortedSet(
           Ordering.natural()
               .onResultOf(
                   new Function<Path, FileTime>() {
                     @Override
                     public FileTime apply(Path path) {
                       try {
                         return getLastModifiedTimeFetcher().getLastModifiedTime(path);
                       } catch (IOException e) {
                         throw new RuntimeException(e);
                       }
                     }
                   })
               .compound(Ordering.natural())
               .reverse());
 }
Exemplo n.º 6
0
  private static Ordering newObjectOrdering(boolean ascending) {
    if (ascending) {
      return Ordering.from(
          new Comparator<Comparable>() {
            @Override
            public int compare(@Nullable Comparable left, @Nullable Comparable right) {
              if (left == null) {
                return 1;
              }
              if (right == null) {
                return -1;
              }

              return left.compareTo(right);
            }
          });
    }
    return Ordering.from(
        new Comparator<Comparable>() {
          @Override
          public int compare(@Nullable Comparable left, @Nullable Comparable right) {
            if (left == null) {
              return 1;
            }
            if (right == null) {
              return -1;
            }

            return -left.compareTo(right);
          }
        });
  }
Exemplo n.º 7
0
  @Test
  public void testIndexTask() throws Exception {
    final Task indexTask =
        new IndexTask(
            null,
            null,
            new IndexTask.IndexIngestionSpec(
                new DataSchema(
                    "foo",
                    null,
                    new AggregatorFactory[] {new DoubleSumAggregatorFactory("met", "met")},
                    new UniformGranularitySpec(
                        Granularity.DAY, null, ImmutableList.of(new Interval("2010-01-01/P2D"))),
                    mapper),
                new IndexTask.IndexIOConfig(new MockFirehoseFactory(false)),
                new IndexTask.IndexTuningConfig(10000, 10, -1, indexSpec)),
            mapper,
            null);

    final Optional<TaskStatus> preRunTaskStatus = tsqa.getStatus(indexTask.getId());
    Assert.assertTrue("pre run task status not present", !preRunTaskStatus.isPresent());

    final TaskStatus mergedStatus = runTask(indexTask);
    final TaskStatus status = ts.getStatus(indexTask.getId()).get();
    final List<DataSegment> publishedSegments = byIntervalOrdering.sortedCopy(mdc.getPublished());
    final List<DataSegment> loggedSegments =
        byIntervalOrdering.sortedCopy(tsqa.getInsertedSegments(indexTask.getId()));

    Assert.assertEquals("statusCode", TaskStatus.Status.SUCCESS, status.getStatusCode());
    Assert.assertEquals(
        "merged statusCode", TaskStatus.Status.SUCCESS, mergedStatus.getStatusCode());
    Assert.assertEquals("segments logged vs published", loggedSegments, publishedSegments);
    Assert.assertEquals("num segments published", 2, mdc.getPublished().size());
    Assert.assertEquals("num segments nuked", 0, mdc.getNuked().size());

    Assert.assertEquals("segment1 datasource", "foo", publishedSegments.get(0).getDataSource());
    Assert.assertEquals(
        "segment1 interval",
        new Interval("2010-01-01/P1D"),
        publishedSegments.get(0).getInterval());
    Assert.assertEquals(
        "segment1 dimensions",
        ImmutableList.of("dim1", "dim2"),
        publishedSegments.get(0).getDimensions());
    Assert.assertEquals(
        "segment1 metrics", ImmutableList.of("met"), publishedSegments.get(0).getMetrics());

    Assert.assertEquals("segment2 datasource", "foo", publishedSegments.get(1).getDataSource());
    Assert.assertEquals(
        "segment2 interval",
        new Interval("2010-01-02/P1D"),
        publishedSegments.get(1).getInterval());
    Assert.assertEquals(
        "segment2 dimensions",
        ImmutableList.of("dim1", "dim2"),
        publishedSegments.get(1).getDimensions());
    Assert.assertEquals(
        "segment2 metrics", ImmutableList.of("met"), publishedSegments.get(1).getMetrics());
  }
Exemplo n.º 8
0
  public void testSortedCopy() {
    List<Integer> unsortedInts = Collections.unmodifiableList(Arrays.asList(5, 0, 3, null, 0, 9));
    List<Integer> sortedInts = numberOrdering.nullsLast().sortedCopy(unsortedInts);
    assertEquals(Arrays.asList(0, 0, 3, 5, 9, null), sortedInts);

    assertEquals(
        Collections.emptyList(), numberOrdering.sortedCopy(Collections.<Integer>emptyList()));
  }
Exemplo n.º 9
0
  private static void verifyMergeSorted(
      Iterable<Iterable<Integer>> iterables, Iterable<Integer> unsortedExpected) {
    Iterable<Integer> expected = Ordering.natural().sortedCopy(unsortedExpected);

    Iterable<Integer> mergedIterator = Iterables.mergeSorted(iterables, Ordering.natural());

    assertEquals(Lists.newLinkedList(expected), Lists.newLinkedList(mergedIterator));
  }
Exemplo n.º 10
0
 @Override
 Ordering sortFieldOrdering(boolean ascending) {
   Ordering<Integer> ordering = Ordering.<Integer>natural().nullsLast();
   if (!ascending) {
     ordering = ordering.reverse();
   }
   return ordering;
 }
Exemplo n.º 11
0
 void testBinarySearch() {
   for (int i = 0; i < strictlyOrderedList.size(); i++) {
     assertEquals(i, ordering.binarySearch(strictlyOrderedList, strictlyOrderedList.get(i)));
   }
   List<T> newList = Lists.newArrayList(strictlyOrderedList);
   T valueNotInList = newList.remove(1);
   assertEquals(-2, ordering.binarySearch(newList, valueNotInList));
 }
Exemplo n.º 12
0
 @Override
 Ordering sortFieldOrdering(boolean ascending) {
   Ordering<String> ordering = Ordering.from(String.CASE_INSENSITIVE_ORDER);
   if (!ascending) {
     ordering = ordering.reverse();
   }
   return ordering;
 }
Exemplo n.º 13
0
 public void testBuilder_orderEntriesByValue_usedTwiceFails() {
   ImmutableBiMap.Builder<String, Integer> builder =
       new Builder<String, Integer>().orderEntriesByValue(Ordering.natural());
   try {
     builder.orderEntriesByValue(Ordering.natural());
     fail("Expected IllegalStateException");
   } catch (IllegalStateException expected) {
   }
 }
Exemplo n.º 14
0
 public void testIsOrdered() {
   assertFalse(numberOrdering.isOrdered(asList(5, 3, 0, 9)));
   assertFalse(numberOrdering.isOrdered(asList(0, 5, 3, 9)));
   assertTrue(numberOrdering.isOrdered(asList(0, 3, 5, 9)));
   assertTrue(numberOrdering.isOrdered(asList(0, 0, 3, 3)));
   assertTrue(numberOrdering.isOrdered(asList(0, 3)));
   assertTrue(numberOrdering.isOrdered(Collections.singleton(1)));
   assertTrue(numberOrdering.isOrdered(Collections.<Integer>emptyList()));
 }
    @Override
    public int compare(ProductType o1, ProductType o2) {
      int result = NULLS_LAST_DIMENSION_TYPE_ORDERING.compare(o1.getType(), o2.getType());
      if (result != 0) {
        return result;
      }

      return NULLS_LAST_CASE_INSENSITIVE_ORDERING.compare(o1.getValue(), o2.getValue());
    }
Exemplo n.º 16
0
  public void testNullsLast() {
    Ordering<Integer> ordering = Ordering.natural().nullsLast();
    Helpers.testComparator(ordering, 0, 1, Integer.MAX_VALUE, null);

    new EqualsTester()
        .addEqualityGroup(ordering, Ordering.natural().nullsLast())
        .addEqualityGroup(numberOrdering.nullsLast())
        .addEqualityGroup(Ordering.natural())
        .testEquals();
  }
Exemplo n.º 17
0
  public void testReverse() {
    Ordering<Number> reverseOrder = numberOrdering.reverse();
    Helpers.testComparator(reverseOrder, Integer.MAX_VALUE, 1, 0, -1, Integer.MIN_VALUE);

    new EqualsTester()
        .addEqualityGroup(reverseOrder, numberOrdering.reverse())
        .addEqualityGroup(Ordering.natural().reverse())
        .addEqualityGroup(Collections.reverseOrder())
        .testEquals();
  }
Exemplo n.º 18
0
    void testSortedCopy() {
      List<T> shuffledList = Lists.newArrayList(strictlyOrderedList);
      shuffledList = shuffledCopy(shuffledList, new Random(5));

      assertEquals(strictlyOrderedList, ordering.sortedCopy(shuffledList));

      if (!strictlyOrderedList.contains(null)) {
        assertEquals(strictlyOrderedList, ordering.immutableSortedCopy(shuffledList));
      }
    }
Exemplo n.º 19
0
  public void testIterableMinAndMax() {
    List<Integer> ints = Lists.newArrayList(5, 3, 0, 9);
    assertEquals(9, (int) numberOrdering.max(ints));
    assertEquals(0, (int) numberOrdering.min(ints));

    // when the values are the same, the first argument should be returned
    Integer a = new Integer(4);
    Integer b = new Integer(4);
    ints = Lists.newArrayList(a, b, b);
    assertSame(a, numberOrdering.max(ints));
    assertSame(a, numberOrdering.min(ints));
  }
Exemplo n.º 20
0
  public void testParameterMinAndMax() {
    assertEquals(5, (int) numberOrdering.max(3, 5));
    assertEquals(5, (int) numberOrdering.max(5, 3));
    assertEquals(3, (int) numberOrdering.min(3, 5));
    assertEquals(3, (int) numberOrdering.min(5, 3));

    // when the values are the same, the first argument should be returned
    Integer a = new Integer(4);
    Integer b = new Integer(4);
    assertSame(a, numberOrdering.max(a, b));
    assertSame(a, numberOrdering.min(a, b));
  }
Exemplo n.º 21
0
 @Test(groups = "ordering")
 public void testAdHocOrdering() {
   Ordering<String> byLengthOrdering =
       new Ordering<String>() {
         public int compare(String left, String right) {
           return (left.length() - right.length());
         }
       };
   assertEquals(
       byLengthOrdering.max(hanSolo.getName(), lukeSkywalker.getName(), princessLeia.getName()),
       "Luke Skywalker");
 }
Exemplo n.º 22
0
 @SuppressWarnings("unchecked")
 @Test
 public void testGroupSortNoEffect() {
   List<Tuple2<Integer, Integer>> pairs =
       Lists.newArrayList(tuple2(1, 2), tuple2(2, 3), tuple2(1, 3), tuple2(3, 1), tuple2(2, 1));
   JavaPairRDD<Integer, Integer> p = jsc().parallelizePairs(pairs);
   GroupSorted<Integer, Integer> gs =
       new GroupSorted(p, new HashPartitioner(2), Ordering.natural());
   GroupSorted<Integer, Integer> gs1 =
       new GroupSorted(gs, new HashPartitioner(2), Ordering.natural());
   Assert.assertTrue(JavaPairRDD.toRDD(gs) == JavaPairRDD.toRDD(gs1));
 }
  /**
   * Get a flat list of validation messages
   *
   * <p>One message has the form:
   *
   * <pre>
   *     #/pointer/here: message here
   * </pre>
   *
   * <p>The list is sorted by pointer.
   *
   * @return the list of messages
   */
  public List<String> getMessages() {
    final Iterable<JsonPointer> paths = Ordering.natural().sortedCopy(msgMap.keySet());

    final ImmutableList.Builder<String> builder = ImmutableList.builder();

    List<Message> messages;

    for (final JsonPointer path : paths) {
      messages = MESSAGE_ORDER.sortedCopy(msgMap.get(path));
      for (final Message msg : messages) builder.add(path + ": " + msg);
    }

    return builder.build();
  }
  /**
   * @param executorService the {@link ScheduledExecutorService} to provide the threads that execute
   *     the operations for reliability.
   */
  public IncomingMessageReliabilityHandler(ScheduledExecutorService executorService) {
    this.shutdown = false;
    this.emptyAcknowledgementSchedule =
        TreeMultimap.create(
            Ordering.<Long>natural(), Ordering.<IncomingReliableMessageExchange>arbitrary());

    this.ongoingMessageExchanges = HashBasedTable.create();

    executorService.scheduleAtFixedRate(
        new ReliabilityTask(),
        RELIABILITY_TASK_PERIOD_MILLIS,
        RELIABILITY_TASK_PERIOD_MILLIS,
        TimeUnit.MILLISECONDS);
  }
Exemplo n.º 25
0
    @SuppressWarnings("unchecked") // generic arrays and unchecked cast
    void testMinAndMax() {
      List<T> shuffledList = Lists.newArrayList(strictlyOrderedList);
      shuffledList = shuffledCopy(shuffledList, new Random(5));

      T min = strictlyOrderedList.get(0);
      T max = strictlyOrderedList.get(strictlyOrderedList.size() - 1);

      T first = shuffledList.get(0);
      T second = shuffledList.get(1);
      T third = shuffledList.get(2);
      T[] rest = shuffledList.subList(3, shuffledList.size()).toArray(emptyArray);

      assertEquals(min, ordering.min(shuffledList));
      assertEquals(min, ordering.min(shuffledList.iterator()));
      assertEquals(min, ordering.min(first, second, third, rest));
      assertEquals(min, ordering.min(min, max));
      assertEquals(min, ordering.min(max, min));

      assertEquals(max, ordering.max(shuffledList));
      assertEquals(max, ordering.max(shuffledList.iterator()));
      assertEquals(max, ordering.max(first, second, third, rest));
      assertEquals(max, ordering.max(min, max));
      assertEquals(max, ordering.max(max, min));
    }
  @Override
  public void validate(
      final ValidationContext context, final ValidationReport report, final JsonNode instance) {
    final Set<String> fields = Sets.newHashSet(instance.fieldNames());
    final Set<String> missing = Sets.difference(required, fields);

    if (missing.isEmpty()) return;

    final Message.Builder msg =
        newMsg()
            .addInfo("required", Ordering.natural().sortedCopy(required))
            .addInfo("missing", Ordering.natural().sortedCopy(missing))
            .setMessage("required property(ies) not found");
    report.addMessage(msg.build());
  }
Exemplo n.º 27
0
 /**
  * Returns the minimal range that {@linkplain Range#contains(Comparable) contains} all of the
  * given values. The returned range is {@linkplain BoundType#CLOSED closed} on both ends.
  *
  * @throws ClassCastException if the parameters are not <i>mutually comparable</i>
  * @throws NoSuchElementException if {@code values} is empty
  * @throws NullPointerException if any of {@code values} is null
  */
 public static <C extends Comparable<?>> Range<C> encloseAll(Iterable<C> values) {
   checkNotNull(values);
   if (values instanceof ContiguousSet) {
     return ((ContiguousSet<C>) values).range();
   }
   Iterator<C> valueIterator = values.iterator();
   C min = checkNotNull(valueIterator.next());
   C max = min;
   while (valueIterator.hasNext()) {
     C value = checkNotNull(valueIterator.next());
     min = Ordering.natural().min(min, value);
     max = Ordering.natural().max(max, value);
   }
   return closed(min, max);
 }
Exemplo n.º 28
0
  // A more limited test than the one that follows, but this one uses the
  // actual public API.
  public void testArbitrary_withoutCollisions() {
    List<Object> list = Lists.newArrayList();
    for (int i = 0; i < 50; i++) {
      list.add(new Object());
    }

    Ordering<Object> arbitrary = Ordering.arbitrary();
    Collections.sort(list, arbitrary);

    // Now we don't care what order it's put the list in, only that
    // comparing any pair of elements gives the answer we expect.
    Helpers.testComparator(arbitrary, list);

    assertEquals("Ordering.arbitrary()", arbitrary.toString());
  }
Exemplo n.º 29
0
 public Weight(String feature, Vector weights, int n) {
   this.feature = feature;
   // pick out the weight with the largest abs value, but don't forget the sign
   Queue<Category> biggest = new PriorityQueue<Category>(n + 1, Ordering.natural());
   for (Vector.Element element : weights.all()) {
     biggest.add(new Category(element.index(), element.get()));
     while (biggest.size() > n) {
       biggest.poll();
     }
   }
   categories = Lists.newArrayList(biggest);
   Collections.sort(categories, Ordering.natural().reverse());
   value = categories.get(0).weight;
   maxIndex = categories.get(0).index;
 }
Exemplo n.º 30
0
  public void testOnResultOf_natural() {
    Comparator<String> comparator =
        Ordering.natural().onResultOf(StringLengthFunction.StringLength);
    assertTrue(comparator.compare("to", "be") == 0);
    assertTrue(comparator.compare("or", "not") < 0);
    assertTrue(comparator.compare("that", "to") > 0);

    new EqualsTester()
        .addEqualityGroup(
            comparator, Ordering.natural().onResultOf(StringLengthFunction.StringLength))
        .addEqualityGroup(DECREASING_INTEGER)
        .testEquals();
    reserializeAndAssert(comparator);
    assertEquals("Ordering.natural().onResultOf(StringLength)", comparator.toString());
  }