Пример #1
0
 public void testComplexBuilder() {
   List<Integer> colorElem = asList(0x00, 0x33, 0x66, 0x99, 0xCC, 0xFF);
   // javac won't compile this without "this.<Integer>"
   ImmutableSet.Builder<Integer> webSafeColorsBuilder = this.<Integer>builder();
   for (Integer red : colorElem) {
     for (Integer green : colorElem) {
       for (Integer blue : colorElem) {
         webSafeColorsBuilder.add((red << 16) + (green << 8) + blue);
       }
     }
   }
   ImmutableSet<Integer> webSafeColors = webSafeColorsBuilder.build();
   assertEquals(216, webSafeColors.size());
   Integer[] webSafeColorArray = webSafeColors.toArray(new Integer[webSafeColors.size()]);
   assertEquals(0x000000, (int) webSafeColorArray[0]);
   assertEquals(0x000033, (int) webSafeColorArray[1]);
   assertEquals(0x000066, (int) webSafeColorArray[2]);
   assertEquals(0x003300, (int) webSafeColorArray[6]);
   assertEquals(0x330000, (int) webSafeColorArray[36]);
   ImmutableSet<Integer> addedColor = webSafeColorsBuilder.add(LAST_COLOR_ADDED).build();
   assertEquals(
       "Modifying the builder should not have changed any already built sets",
       216,
       webSafeColors.size());
   assertEquals("the new array should be one bigger than webSafeColors", 217, addedColor.size());
   Integer[] appendColorArray = addedColor.toArray(new Integer[addedColor.size()]);
   assertEquals(getComplexBuilderSetLastElement(), (int) appendColorArray[216]);
 }
Пример #2
0
  private static <K, V> ImmutableSetMultimap<K, V> copyOf(
      Multimap<? extends K, ? extends V> multimap, Comparator<? super V> valueComparator) {
    checkNotNull(multimap); // eager for GWT
    if (multimap.isEmpty() && valueComparator == null) {
      return of();
    }

    if (multimap instanceof ImmutableSetMultimap) {
      @SuppressWarnings("unchecked") // safe since multimap is not writable
      ImmutableSetMultimap<K, V> kvMultimap = (ImmutableSetMultimap<K, V>) multimap;
      if (!kvMultimap.isPartialView()) {
        return kvMultimap;
      }
    }

    ImmutableMap.Builder<K, ImmutableSet<V>> builder = ImmutableMap.builder();
    int size = 0;

    for (Entry<? extends K, ? extends Collection<? extends V>> entry :
        multimap.asMap().entrySet()) {
      K key = entry.getKey();
      Collection<? extends V> values = entry.getValue();
      ImmutableSet<V> set =
          (valueComparator == null)
              ? ImmutableSet.copyOf(values)
              : ImmutableSortedSet.copyOf(valueComparator, values);
      if (!set.isEmpty()) {
        builder.put(key, set);
        size += set.size();
      }
    }

    return new ImmutableSetMultimap<K, V>(builder.build(), size, valueComparator);
  }
Пример #3
0
  @Test
  public void searchBioentityIdentifiers() {
    ImmutableSet<String> result =
        subject.searchBioentityIdentifiers(query, condition, species.getReferenceName());

    assertThat(result.size(), greaterThan(0));
  }
Пример #4
0
  /** Verify that owners are correctly detected: - one owner, multiple inputs */
  @Test
  public void verifyInputsWithOneOwnerAreCorrectlyReported()
      throws CmdLineException, IOException, InterruptedException {
    FakeProjectFilesystem filesystem =
        new FakeProjectFilesystem() {
          @Override
          public File getFileForRelativePath(String pathRelativeToProjectRoot) {
            return new ExistingFile(getRootPath(), pathRelativeToProjectRoot);
          }
        };

    ImmutableSet<String> inputs =
        ImmutableSet.of(
            "java/somefolder/badfolder/somefile.java",
            "java/somefolder/perfect.java",
            "com/test/subtest/random.java");
    ImmutableSet<Path> inputPaths = MorePaths.asPaths(inputs);

    BuildTarget target = BuildTargetFactory.newInstance("//base:name");
    TargetNode<?> targetNode = createTargetNode(target, inputPaths);

    CommandRunnerParams params = createAuditOwnerCommandRunnerParams(filesystem);
    AuditOwnerCommand.OwnersReport report =
        AuditOwnerCommand.generateOwnersReport(params, targetNode, inputs, false);
    assertTrue(report.nonFileInputs.isEmpty());
    assertTrue(report.nonExistentInputs.isEmpty());
    assertTrue(report.inputsWithNoOwners.isEmpty());

    assertEquals(inputs.size(), report.owners.size());
    assertTrue(report.owners.containsKey(targetNode));
    assertEquals(targetNode.getInputs(), report.owners.get(targetNode));
  }
Пример #5
0
  public static void testSuppressingSuppressorIfPossible() throws IOException {
    // can't test the JDK7 suppressor when not running on JDK7
    if (!Closer.SuppressingSuppressor.isAvailable()) {
      return;
    }

    Closer closer = new Closer(new Closer.SuppressingSuppressor());

    IOException thrownException = new IOException();
    IOException c1Exception = new IOException();
    RuntimeException c2Exception = new RuntimeException();

    TestCloseable c1 = closer.register(TestCloseable.throwsOnClose(c1Exception));
    TestCloseable c2 = closer.register(TestCloseable.throwsOnClose(c2Exception));
    try {
      try {
        throw thrownException;
      } catch (Throwable e) {
        throw closer.rethrow(thrownException, IOException.class);
      } finally {
        assertEquals(0, getSuppressed(thrownException).length);
        closer.close();
      }
    } catch (IOException expected) {
      assertSame(thrownException, expected);
    }

    assertTrue(c1.isClosed());
    assertTrue(c2.isClosed());

    ImmutableSet<Throwable> suppressed = ImmutableSet.copyOf(getSuppressed(thrownException));
    assertEquals(2, suppressed.size());

    assertEquals(ImmutableSet.of(c1Exception, c2Exception), suppressed);
  }
Пример #6
0
 public boolean isIdentical(BuildEntity b) {
   ImmutableSet<BuildProperty> oprops = b.getProperties();
   if (oprops.size() != getProperties().size()) {
     return false;
   }
   for (BuildProperty prop : properties) {
     boolean foundIdentical = false;
     for (BuildProperty oprop : oprops) {
       if (oprop.isIdentical(prop)) {
         foundIdentical = true;
       }
     }
     if (!foundIdentical) {
       return false;
     }
   }
   return Objects.equal(promotions, b.promotions)
       && StringUtils.equals(buildName, b.buildName)
       && StringUtils.equals(buildNumber, b.buildNumber)
       && buildDate == b.buildDate
       && StringUtils.equals(ciUrl, b.ciUrl)
       && created == b.created
       && StringUtils.equals(createdBy, b.createdBy)
       && modified == b.modified
       && StringUtils.equals(modifiedBy, b.modifiedBy);
 }
Пример #7
0
  @Test
  public void fetchExperimentTypes3() {
    ImmutableSet<String> result =
        subject.fetchExperimentTypes(query, condition, species.getReferenceName());

    assertThat(result.size(), greaterThan(0));
  }
Пример #8
0
 public boolean isInterfaceDefinition() {
   if (objs == null || objs.size() > 1) {
     return false;
   }
   FunctionType ft = Iterables.getOnlyElement(objs).getFunType();
   return ft != null && ft.isInterfaceDefinition();
 }
Пример #9
0
    @Value.Lazy
    public List<TypeElement> includedTypes() {
      Optional<IncludeMirror> includes = include();

      ImmutableList<TypeMirror> typeMirrors =
          includes.isPresent()
              ? ImmutableList.copyOf(includes.get().valueMirror())
              : ImmutableList.<TypeMirror>of();

      FluentIterable<TypeElement> typeElements =
          FluentIterable.from(typeMirrors)
              .filter(DeclaredType.class)
              .transform(DeclatedTypeToElement.FUNCTION);

      ImmutableSet<String> uniqueTypeNames =
          typeElements.filter(IsPublic.PREDICATE).transform(ElementToName.FUNCTION).toSet();

      if (uniqueTypeNames.size() != typeMirrors.size()) {
        report()
            .annotationNamed(IncludeMirror.simpleName())
            .warning(
                "Some types were ignored, non-supported for inclusion: duplicates, non declared reference types, non-public");
      }

      return typeElements.toList();
    }
  /**
   * Returns a scenario definition created from all possible combinations of the mappings.
   * <p>
   * The mappings can have any number of perturbations, they do not need to have the same number as each other.
   * Each scenario contain one perturbation from each mapping. One scenario is created for each
   * possible combination of perturbations formed by taking one from each mapping.
   * <p>
   * The number of scenarios in the definition will be equal to the product of the number of perturbations
   * in the mappings.
   * <p>
   * Given three mappings, A, B and C, each containing two perturbations, 1 and 2, there will be eight
   * scenarios generated:
   * <pre>
   * |            |   A  |   B  |   C  |
   * |------------|------|------|------|
   * | Scenario 1 | A[1] | B[1] | C[1] |
   * | Scenario 2 | A[1] | B[1] | C[2] |
   * | Scenario 3 | A[1] | B[2] | C[1] |
   * | Scenario 4 | A[1] | B[2] | C[2] |
   * | Scenario 5 | A[2] | B[1] | C[1] |
   * | Scenario 6 | A[2] | B[1] | C[2] |
   * | Scenario 7 | A[2] | B[2] | C[1] |
   * | Scenario 8 | A[2] | B[2] | C[2] |
   * </pre>
   * For example, consider the following perturbation mappings:
   * <ul>
   *   <li>Filter: USD Curves, Shocks: [-10bp, 0, +10bp]</li>
   *   <li>Filter: EUR/USD Rate, Shocks: [+5%, 0, -5%]</li>
   * </ul>
   * The scenario definition would contain the following nine scenarios:
   * <pre>
   * |            | USD Curves | EUR/USD Rate |
   * |------------|------------|--------------|
   * | Scenario 1 |     -10bp  |     +5%      |
   * | Scenario 2 |     -10bp  |      0       |
   * | Scenario 3 |     -10bp  |     -5%      |
   * | Scenario 4 |       0    |     +5%      |
   * | Scenario 5 |       0    |      0       |
   * | Scenario 6 |       0    |     -5%      |
   * | Scenario 7 |     +10bp  |     +5%      |
   * | Scenario 8 |     +10bp  |      0       |
   * | Scenario 9 |     +10bp  |     -5%      |
   *
   * @param mappings  the filters and perturbations that define the scenarios. They can contain any number
   *   of perturbations, and they do not need to have the same number of perturbations
   * @return a scenario definition containing the perturbations in the mappings
   */
  public static ScenarioDefinition ofAllCombinations(
      ImmutableSet<String> scenarioNames, List<? extends PerturbationMapping<?>> mappings) {

    ArgumentChecker.notEmpty(scenarioNames, "scenarioNames");
    ArgumentChecker.notEmpty(mappings, "mappings");
    int numScenarios = countScenarios(mappings, true);

    if (numScenarios != scenarioNames.size()) {
      throw new IllegalArgumentException(
          "The number of scenario names provided is "
              + scenarioNames.size()
              + " but "
              + "the number of scenarios is "
              + numScenarios);
    }
    return new ScenarioDefinition(createScenarios(scenarioNames, mappings, true));
  }
Пример #11
0
 public Optional<Charset> charset() {
   ImmutableSet<String> charsetValues = ImmutableSet.copyOf(this.parameters.get("charset"));
   switch (charsetValues.size()) {
     case 0:
       return Optional.absent();
     case 1:
       return Optional.of(Charset.forName((String) Iterables.getOnlyElement(charsetValues)));
   }
   throw new IllegalStateException("Multiple charset values defined: " + charsetValues);
 }
Пример #12
0
  @Test
  public void should_generate_unique_integers() {
    int numberCount = 10;
    NumberGenerator generator = new NumberGenerator();

    ArrayList<Integer> numberList = generator.generate1(numberCount);

    ImmutableSet<Integer> set = ImmutableSet.copyOf(numberList);
    assertThat(set.size(), is(numberList.size()));
  }
  public TermsShortOrdinalsFacetCollector(
      String facetName,
      String fieldName,
      int size,
      TermsFacet.ComparatorType comparatorType,
      boolean allTerms,
      SearchContext context,
      ImmutableSet<String> excluded) {
    super(facetName);
    this.fieldDataCache = context.fieldDataCache();
    this.size = size;
    this.comparatorType = comparatorType;
    this.numberOfShards = context.numberOfShards();

    MapperService.SmartNameFieldMappers smartMappers = context.smartFieldMappers(fieldName);
    if (smartMappers == null || !smartMappers.hasMapper()) {
      throw new ElasticSearchIllegalArgumentException(
          "Field ["
              + fieldName
              + "] doesn't have a type, can't run terms short facet collector on it");
    }
    // add type filter if there is exact doc mapper associated with it
    if (smartMappers.explicitTypeInNameWithDocMapper()) {
      setFilter(context.filterCache().cache(smartMappers.docMapper().typeFilter()));
    }

    if (smartMappers.mapper().fieldDataType() != FieldDataType.DefaultTypes.SHORT) {
      throw new ElasticSearchIllegalArgumentException(
          "Field ["
              + fieldName
              + "] is not of short type, can't run terms short facet collector on it");
    }

    this.indexFieldName = smartMappers.mapper().names().indexName();
    this.fieldDataType = smartMappers.mapper().fieldDataType();

    if (excluded == null || excluded.isEmpty()) {
      this.excluded = null;
    } else {
      this.excluded = new TShortHashSet(excluded.size());
      for (String s : excluded) {
        this.excluded.add(Short.parseShort(s));
      }
    }

    // minCount is offset by -1
    if (allTerms) {
      minCount = -1;
    } else {
      minCount = 0;
    }

    this.aggregators = new ArrayList<ReaderAggregator>(context.searcher().subReaders().length);
  }
Пример #14
0
  @Override
  public ImmutableSet<IComponent> getComponents() {
    // Clones all of the components.
    List<IComponent> clonedList = new ArrayList<IComponent>(components.size());
    for (IComponent component : this.components) {
      IComponent clone = (IComponent) component.clone();

      clonedList.add(clone);
    }
    return ImmutableSet.copyOf(clonedList);
  }
Пример #15
0
 static ImmutableSet<Parameter> forParameterList(List<? extends VariableElement> variables) {
   ImmutableSet.Builder<Parameter> builder = ImmutableSet.builder();
   Set<String> names = Sets.newHashSetWithExpectedSize(variables.size());
   for (VariableElement variable : variables) {
     Parameter parameter = forVariableElement(variable);
     checkArgument(names.add(parameter.name));
     builder.add(parameter);
   }
   ImmutableSet<Parameter> parameters = builder.build();
   checkArgument(variables.size() == parameters.size());
   return parameters;
 }
Пример #16
0
  @Test
  public void testListSubnets() {
    ImmutableSet<Subnet> subnets = api().list().toSet();
    getAnonymousLogger().info("subnets: " + subnets.size());

    for (Subnet subnet : subnets) {
      checkSubnet(subnet);
      assertEquals(
          api().filter(new SubnetFilterBuilder().subnetId(subnet.getSubnetId()).build()).get(0),
          subnet);
    }
  }
Пример #17
0
 public void testBuilderWithDuplicateElements() {
   ImmutableSet<String> set =
       this.<String>builder()
           .add("a")
           .add("a", "a")
           .add("a", "a", "a")
           .add("a", "a", "a", "a")
           .build();
   assertTrue(set.contains("a"));
   assertFalse(set.contains("b"));
   assertEquals(1, set.size());
 }
Пример #18
0
  /**
   * Hydrates configured {@literal @CmdLine} arg fields and selects a desired set with the supplied
   * {@code filter}.
   *
   * @param configuration The configuration to find candidate {@literal @CmdLine} arg fields in.
   * @param filter A predicate to select fields with.
   * @return The desired hydrated {@literal @CmdLine} arg fields and optional {@literal @Positional}
   *     arg field.
   */
  static ArgumentInfo fromConfiguration(Configuration configuration, Predicate<Field> filter) {

    ImmutableSet<Field> positionalFields =
        ImmutableSet.copyOf(filterFields(configuration.positionalInfo(), filter));

    if (positionalFields.size() > 1) {
      throw new IllegalArgumentException(
          String.format(
              "Found %d fields marked for @Positional Args after applying filter - "
                  + "only 1 is allowed:\n\t%s",
              positionalFields.size(), Joiner.on("\n\t").join(positionalFields)));
    }

    Optional<PositionalInfo> positionalInfo =
        Optional.fromNullable(
            Iterables.getOnlyElement(
                Iterables.transform(positionalFields, TO_POSITIONALINFO), null));
    Iterable<OptionInfo> optionInfos =
        Iterables.transform(filterFields(configuration.optionInfo(), filter), TO_OPTIONINFO);
    return new ArgumentInfo(positionalInfo, optionInfos);
  }
Пример #19
0
 public FunctionType getFunType() {
   if (objs == null) {
     return null;
   }
   if (objs.size() == 1) { // The common case is fast
     return Iterables.getOnlyElement(objs).getFunType();
   }
   FunctionType result = FunctionType.TOP_FUNCTION;
   for (ObjectType obj : objs) {
     result = FunctionType.meet(result, obj.getFunType());
   }
   return result;
 }
  @SuppressWarnings("deprecation")
  @Test
  public void testExceptionsAreGood() throws Exception {
    ImmutableSet<ClassInfo> classes =
        ClassPath.from(Thread.currentThread().getContextClassLoader())
            .getTopLevelClasses(BaseServerResponseException.class.getPackage().getName());
    assertTrue(classes.size() > 5);

    for (ClassInfo classInfo : classes) {
      ourLog.info("Scanning {}", classInfo.getName());

      Class<?> next = Class.forName(classInfo.getName());
      assertNotNull(next);

      if (next == getClass()) {
        continue;
      }
      if (next == BaseServerResponseException.class) {
        continue;
      }
      if (next == UnclassifiedServerFailureException.class) {
        continue;
      }
      if (next == ResourceVersionNotSpecifiedException.class) {
        // This one is deprocated
        continue;
      }

      assertTrue(
          "Type " + next + " is not registered",
          BaseServerResponseException.isExceptionTypeRegistered(next));

      if (next == AuthenticationException.class) {
        continue;
      }

      try {
        next.getConstructor(String.class, IBaseOperationOutcome.class);
      } catch (NoSuchMethodException e) {
        fail(
            classInfo.getName()
                + " has no constructor with params: (String, IBaseOperationOutcome)");
      }
    }
  }
 private ImmutableSet<String> appendDefaultDBPriv(
     ImmutableSet<String> privileges, Authorizable[] authorizables) {
   // Only for switch db
   if ((authorizables != null)
       && (authorizables.length == 4)
       && (authorizables[2].getName().equals("+"))) {
     if ((privileges.size() == 1) && hasOnlyServerPrivilege(privileges.asList().get(0))) {
       // Assuming authorizable[0] will always be the server
       // This Code is only reachable only when user fires a 'use default'
       // and the user has a privilege on atleast 1 privilized Object
       String defaultPriv =
           "Server="
               + authorizables[0].getName()
               + "->Db=default->Table=*->Column=*->action=select";
       HashSet<String> newPrivs = Sets.newHashSet(defaultPriv);
       return ImmutableSet.copyOf(newPrivs);
     }
   }
   return privileges;
 }
  /**
   * Returns a scenario definition containing the perturbations in {@code mappings}.
   * <p>
   * Each mapping must contain the same number of perturbations. The definition will contain the
   * same number of scenarios as the number of perturbations in each mapping.
   * <p>
   * The first scenario contains the first perturbation from each mapping, the second scenario contains
   * the second perturbation from each mapping, and so on.
   * <p>
   * The set of scenario names must contain the same number of elements as the mappings.
   * <p>
   * Given three mappings, A, B and C, each containing two perturbations, 1 and 2, there will be two
   * scenarios generated:
   * <pre>
   * |            |  A   |  B   |  C   |
   * |------------|------|------|------|
   * | Scenario 1 | A[1] | B[1] | C[1] |
   * | Scenario 2 | A[2] | B[2] | C[2] |
   * </pre>
   * For example, consider the following perturbation mappings:
   * <ul>
   *   <li>Filter: USD Curves, Shocks: [-10bp, 0, +10bp]</li>
   *   <li>Filter: EUR/USD Rate, Shocks: [+5%, 0, -5%]</li>
   * </ul>
   * The scenario definition would contain the following three scenarios:
   * <pre>
   * |            | USD Curves | EUR/USD Rate |
   * |------------|------------|--------------|
   * | Scenario 1 |     -10bp  |     +5%      |
   * | Scenario 2 |       0    |      0       |
   * | Scenario 3 |     +10bp  |     -5%      |
   *
   * @param mappings  the filters and perturbations that define the scenario. Each mapping must contain the same
   *   number of perturbations
   * @param scenarioNames  the names of the scenarios. This must be the same size as the list of perturbations
   *   in each mapping
   * @return a scenario definition containing the perturbations in the mappings
   */
  public static ScenarioDefinition ofMappings(
      ImmutableSet<String> scenarioNames, List<? extends PerturbationMapping<?>> mappings) {

    ArgumentChecker.notNull(scenarioNames, "scenarioNames");
    int numScenarios = scenarioNames.size();

    for (int i = 0; i < mappings.size(); i++) {
      if (mappings.get(i).getPerturbations().size() != numScenarios) {
        throw new IllegalArgumentException(
            "Each mapping must contain the same number of perturbations as there are scenarios. There are "
                + numScenarios
                + " scenarios, mapping "
                + i
                + " has "
                + mappings.get(i).getPerturbations().size()
                + " perturbations.");
      }
    }
    return new ScenarioDefinition(createScenarios(scenarioNames, mappings, false));
  }
Пример #23
0
  private static String generateLanguageAcceptCheck(
      String expression,
      ImmutableSet<CtClass> acceptedLanguages,
      HashMap<CtClass, String> acceptedLanguageMap) {
    if (acceptedLanguages.isEmpty()) {
      return "false";
    }

    StringBuilder sb = new StringBuilder();
    int i = 0;
    for (CtClass l : acceptedLanguages) {
      sb.append(expression).append(".isAcceptedBy(").append(acceptedLanguageMap.get(l)).append(")");
      if (i < acceptedLanguages.size() - 1) {
        sb.append(" || ");
      }

      i++;
    }

    return sb.toString();
  }
Пример #24
0
 @Test
 public void fetchExperimentTypes1() {
   ImmutableSet<String> result = subject.fetchExperimentTypes("ENSG00000006432");
   assertThat(result.size(), greaterThan(0));
 }
Пример #25
0
 public FunctionType getFunTypeIfSingletonObj() {
   if (mask != NON_SCALAR_MASK || objs.size() > 1) {
     return null;
   }
   return Iterables.getOnlyElement(objs).getFunType();
 }
Пример #26
0
  private BLANCResult scoreSets(
      final Iterable<Set<Object>> predicted, final Iterable<Set<Object>> gold) {

    final Multimap<Object, Set<Object>> predictedItemToGroup =
        CollectionUtils.makeSetElementsToContainersMultimap(predicted);
    final Multimap<Object, Set<Object>> goldItemToGroup =
        CollectionUtils.makeSetElementsToContainersMultimap(gold);

    final Set<Object> keyItems = goldItemToGroup.keySet();
    final Set<Object> responseItems = predictedItemToGroup.keySet();
    final ImmutableSet<Object> itemsInBoth =
        Sets.intersection(keyItems, responseItems).immutableCopy();

    // |C_k \cap C_r|
    int corefLinksInBoth = 0;
    // |C_k|
    int corefLinksInKey = 0;
    // |C_r|
    int corefLinksInResponse = 0;
    // |N_K \cap N_r|
    int nonCorefInBoth = 0;
    // |N_k|
    int nonCorefLinksInKey = 0;
    // |N_r|
    int nonCorefLinksInResponse = 0;

    final Set<Object> allItems = Sets.union(responseItems, keyItems).immutableCopy();

    for (final Object item : allItems) {
      final boolean inKey = keyItems.contains(item);
      final boolean inResponse = responseItems.contains(item);

      final Collection<Set<Object>> predictedClusters = predictedItemToGroup.get(item);
      final Collection<Set<Object>> goldClusters = goldItemToGroup.get(item);

      final Predicate<Object> SELF_ADJUSTMENT_FILTER;
      if (useSelfEdges) {
        SELF_ADJUSTMENT_FILTER = Predicates.alwaysTrue();
      } else {
        SELF_ADJUSTMENT_FILTER = not(equalTo(item));
      }
      final int selfAdjustment = useSelfEdges ? 0 : -1;

      final ImmutableSet<Object> predictedNeighbors =
          FluentIterable.from(concat(predictedClusters)).filter(SELF_ADJUSTMENT_FILTER).toSet();
      final ImmutableSet<Object> goldNeighbors =
          FluentIterable.from(concat(goldClusters)).filter(SELF_ADJUSTMENT_FILTER).toSet();

      // The contribution for this item is the size of the intersection
      // of the gold and predicted neighbor sets.
      corefLinksInBoth += Sets.intersection(predictedNeighbors, goldNeighbors).size();
      corefLinksInResponse += predictedNeighbors.size();
      corefLinksInKey += goldNeighbors.size();
      if (inKey) {
        nonCorefLinksInKey += keyItems.size() - goldNeighbors.size() + selfAdjustment;
      }

      if (inResponse) {
        nonCorefLinksInResponse +=
            responseItems.size() - predictedNeighbors.size() + selfAdjustment;
      }

      if (inKey && inResponse) {
        final ImmutableSet<Object> neighborsInEither =
            Sets.union(predictedNeighbors, goldNeighbors).immutableCopy();
        // -1 = don't count this item itself as a link
        nonCorefInBoth += Sets.difference(itemsInBoth, neighborsInEither).size() + selfAdjustment;
      }
    }

    return BLANCResult.fromSetCounts(
        keyItems.equals(responseItems),
        corefLinksInBoth,
        corefLinksInKey,
        corefLinksInResponse,
        nonCorefInBoth,
        nonCorefLinksInKey,
        nonCorefLinksInResponse);
  }
Пример #27
0
 NominalType getNominalTypeIfUnique() {
   if (objs == null || objs.size() > 1) {
     return null;
   }
   return Iterables.getOnlyElement(objs).getNominalType();
 }
Пример #28
0
  public Map<String, Object> toElasticSearchObject(@Nonnull final Meter invalidTimestampMeter) {
    final Map<String, Object> obj =
        Maps.newHashMapWithExpectedSize(REQUIRED_FIELDS.size() + fields.size());

    for (Map.Entry<String, Object> entry : fields.entrySet()) {
      final String key = entry.getKey();

      // Elasticsearch does not allow "." characters in keys since version 2.0.
      // See:
      // https://www.elastic.co/guide/en/elasticsearch/reference/2.0/breaking_20_mapping_changes.html#_field_names_may_not_contain_dots
      if (key != null && key.contains(".")) {
        final String newKey = key.replace('.', KEY_REPLACEMENT_CHAR);

        // If the message already contains the transformed key, we skip the field and emit a
        // warning.
        // This is still not optimal but better than implementing expensive logic with multiple
        // replacement
        // character options. Conflicts should be rare...
        if (!obj.containsKey(newKey)) {
          obj.put(newKey, entry.getValue());
        } else {
          LOG.warn(
              "Keys must not contain a \".\" character! Ignoring field \"{}\"=\"{}\" in message [{}] - Unable to replace \".\" with a \"{}\" because of key conflict: \"{}\"=\"{}\"",
              key,
              entry.getValue(),
              getId(),
              KEY_REPLACEMENT_CHAR,
              newKey,
              obj.get(newKey));
          LOG.debug("Full message with \".\" in message key: {}", this);
        }
      } else {
        if (key != null && obj.containsKey(key)) {
          final String newKey = key.replace(KEY_REPLACEMENT_CHAR, '.');
          // Deliberate warning duplicates because the key with the "." might be transformed before
          // reaching
          // the duplicate original key with a "_". Otherwise we would silently overwrite the
          // transformed key.
          LOG.warn(
              "Keys must not contain a \".\" character! Ignoring field \"{}\"=\"{}\" in message [{}] - Unable to replace \".\" with a \"{}\" because of key conflict: \"{}\"=\"{}\"",
              newKey,
              fields.get(newKey),
              getId(),
              KEY_REPLACEMENT_CHAR,
              key,
              entry.getValue());
          LOG.debug("Full message with \".\" in message key: {}", this);
        }
        obj.put(key, entry.getValue());
      }
    }

    obj.put(FIELD_MESSAGE, getMessage());
    obj.put(FIELD_SOURCE, getSource());

    final Object timestampValue = getField(FIELD_TIMESTAMP);
    DateTime dateTime;
    if (timestampValue instanceof Date) {
      dateTime = new DateTime(timestampValue);
    } else if (timestampValue instanceof DateTime) {
      dateTime = (DateTime) timestampValue;
    } else if (timestampValue instanceof String) {
      // if the timestamp value is a string, we try to parse it in the correct format.
      // we fall back to "now", this avoids losing messages which happen to have the wrong timestamp
      // format
      try {
        dateTime = ES_DATE_FORMAT_FORMATTER.parseDateTime((String) timestampValue);
      } catch (IllegalArgumentException e) {
        LOG.trace(
            "Invalid format for field timestamp '{}' in message {}, forcing to current time.",
            timestampValue,
            getId());
        invalidTimestampMeter.mark();
        dateTime = Tools.nowUTC();
      }
    } else {
      // don't allow any other types for timestamp, force to "now"
      LOG.trace(
          "Invalid type for field timestamp '{}' in message {}, forcing to current time.",
          timestampValue.getClass().getSimpleName(),
          getId());
      invalidTimestampMeter.mark();
      dateTime = Tools.nowUTC();
    }
    if (dateTime != null) {
      obj.put(FIELD_TIMESTAMP, buildElasticSearchTimeFormat(dateTime.withZone(UTC)));
    }

    // Manually converting stream ID to string - caused strange problems without it.
    if (getStreams().isEmpty()) {
      obj.put(FIELD_STREAMS, Collections.emptyList());
    } else {
      final List<String> streamIds = Lists.newArrayListWithCapacity(streams.size());
      for (Stream stream : streams) {
        streamIds.add(stream.getId());
      }
      obj.put(FIELD_STREAMS, streamIds);
    }

    return obj;
  }
Пример #29
0
 @Override
 public int concreteSize() {
   return elements.size();
 }
Пример #30
0
  @Test
  public void fetchExperimentTypesInAnyField() {
    ImmutableSet<String> result = subject.fetchExperimentTypesInAnyField(query);

    assertThat(result.size(), greaterThan(0));
  }