Java Code Examples for com.google.common.collect.ImmutableListMultimap#get()

The following examples show how to use com.google.common.collect.ImmutableListMultimap#get() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ArtifactProblem.java    From cloud-opensource-java with Apache License 2.0 6 votes vote down vote up
/**
 * Returns formatted string describing {@code problems} by removing similar problems per artifact.
 */
public static String formatProblems(Iterable<ArtifactProblem> problems) {
  ImmutableListMultimap<Artifact, ArtifactProblem> artifactToProblems =
      Multimaps.index(problems, ArtifactProblem::getArtifact);

  StringBuilder output = new StringBuilder();
  for (Artifact artifact : artifactToProblems.keySet()) {
    ImmutableList<ArtifactProblem> artifactProblems = artifactToProblems.get(artifact);
    int otherCount = artifactProblems.size() - 1;
    verify(otherCount >= 0, "artifactToProblems should have at least one value for one key");
    ArtifactProblem firstProblem = Iterables.getFirst(artifactProblems, null);
    output.append(firstProblem);
    if (otherCount == 1) {
      output.append(" and a problem on the same artifact.");
    } else if (otherCount > 1) {
      output.append(" and " + otherCount + " problems on the same artifact.");
    }
    output.append("\n");
  }
  return output.toString();
}
 
Example 2
Source File: ProtoRegistry.java    From rejoiner with Apache License 2.0 6 votes vote down vote up
/** Applies the supplied modifications to the GraphQLTypes. */
private static BiMap<String, GraphQLType> modifyTypes(
    BiMap<String, GraphQLType> mapping,
    ImmutableListMultimap<String, TypeModification> modifications) {
  BiMap<String, GraphQLType> result = HashBiMap.create(mapping.size());
  for (String key : mapping.keySet()) {
    if (mapping.get(key) instanceof GraphQLObjectType) {
      GraphQLObjectType val = (GraphQLObjectType) mapping.get(key);
      if (modifications.containsKey(key)) {
        for (TypeModification modification : modifications.get(key)) {
          val = modification.apply(val);
        }
      }
      result.put(key, val);
    } else {
      result.put(key, mapping.get(key));
    }
  }
  return result;
}
 
Example 3
Source File: Manifest.java    From buck with Apache License 2.0 6 votes vote down vote up
private boolean hashesMatch(
    FileHashLoader fileHashLoader,
    SourcePathResolverAdapter resolver,
    ImmutableListMultimap<String, SourcePath> universe,
    int[] hashIndices)
    throws IOException {
  for (int hashIndex : hashIndices) {
    Pair<Integer, HashCode> hashEntry = hashes.get(hashIndex);
    String input = inputs.get(hashEntry.getFirst());
    ImmutableList<SourcePath> candidates = universe.get(input);
    if (candidates.isEmpty()) {
      return false;
    }
    HashCode onDiskHeaderHash;
    try {
      onDiskHeaderHash = hashSourcePathGroup(fileHashLoader, resolver, candidates);
    } catch (NoSuchFileException e) {
      return false;
    }
    HashCode inputHash = hashEntry.getSecond();
    if (!inputHash.equals(onDiskHeaderHash)) {
      return false;
    }
  }
  return true;
}
 
Example 4
Source File: MutableActionGraph.java    From bazel with Apache License 2.0 6 votes vote down vote up
/** Returns items in {@code valueA} that are not in {@code valueB}, ignoring the owner. */
private static Set<Artifact> differenceWithoutOwner(
    Iterable<Artifact> valueA, Iterable<Artifact> valueB) {
  ImmutableSet.Builder<Artifact> diff = new ImmutableSet.Builder<>();

  // Group valueB by exec path for easier checks.
  ImmutableListMultimap<String, Artifact> mapB =
      Streams.stream(valueB)
          .collect(toImmutableListMultimap(Artifact::getExecPathString, Functions.identity()));
  for (Artifact a : valueA) {
    boolean found = false;
    for (Artifact b : mapB.get(a.getExecPathString())) {
      if (a.equalsWithoutOwner(b)) {
        found = true;
        break;
      }
    }
    if (!found) {
      diff.add(a);
    }
  }

  return diff.build();
}
 
Example 5
Source File: EclipseHack.java    From SimpleWeibo with Apache License 2.0 6 votes vote down vote up
@Override public ImmutableList<String> determinePropertyOrder() throws IOException {
  Reader sourceReader;
  try {
    sourceReader = readerProvider.call();
  } catch (Exception e) {
    return ImmutableList.of();
  }
  try {
    String packageName = TypeSimplifier.packageNameOf(type);
    String className = type.getQualifiedName().toString();
    AbstractMethodExtractor extractor = new AbstractMethodExtractor();
    JavaTokenizer tokenizer = new JavaTokenizer(sourceReader);
    ImmutableListMultimap<String, String> methodOrders =
        extractor.abstractMethods(tokenizer, packageName);
    return methodOrders.get(className);
  } finally {
    sourceReader.close();
  }
}
 
Example 6
Source File: EclipseHack.java    From RetroFacebook with Apache License 2.0 6 votes vote down vote up
@Override public ImmutableList<String> determinePropertyOrder() throws IOException {
  Reader sourceReader;
  try {
    sourceReader = readerProvider.call();
  } catch (Exception e) {
    return ImmutableList.of();
  }
  try {
    String packageName = TypeSimplifier.packageNameOf(type);
    String className = type.getQualifiedName().toString();
    AbstractMethodExtractor extractor = new AbstractMethodExtractor();
    JavaTokenizer tokenizer = new JavaTokenizer(sourceReader);
    ImmutableListMultimap<String, String> methodOrders =
        extractor.abstractMethods(tokenizer, packageName);
    return methodOrders.get(className);
  } finally {
    sourceReader.close();
  }
}
 
Example 7
Source File: EclipseHack.java    From RetroFacebook with Apache License 2.0 6 votes vote down vote up
@Override public ImmutableList<String> determinePropertyOrder() throws IOException {
  Reader sourceReader;
  try {
    sourceReader = readerProvider.call();
  } catch (Exception e) {
    return ImmutableList.of();
  }
  try {
    String packageName = TypeSimplifier.packageNameOf(type);
    String className = type.getQualifiedName().toString();
    AbstractMethodExtractor extractor = new AbstractMethodExtractor();
    JavaTokenizer tokenizer = new JavaTokenizer(sourceReader);
    ImmutableListMultimap<String, String> methodOrders =
        extractor.abstractMethods(tokenizer, packageName);
    return methodOrders.get(className);
  } finally {
    sourceReader.close();
  }
}
 
Example 8
Source File: XtendValidator.java    From xtext-xtend with Eclipse Public License 2.0 5 votes vote down vote up
@Check
public void checkMultipleAnnotations(final XtendAnnotationTarget annotationTarget) {
	if (annotationTarget.getAnnotations().size() <= 1 || !isRelevantAnnotationTarget(annotationTarget)) {
		return;
	}
	
	ImmutableListMultimap<String, XAnnotation> groupByIdentifier = Multimaps.index(annotationTarget.getAnnotations(),
			new Function<XAnnotation, String>() {
				@Override
				public String apply(XAnnotation input) {
					return input.getAnnotationType().getIdentifier();
				}
			});

	for (String qName : groupByIdentifier.keySet()) {
		ImmutableList<XAnnotation> sameType = groupByIdentifier.get(qName);
		if (sameType.size() > 1) {
			JvmType type = sameType.get(0).getAnnotationType();
			if (type instanceof JvmAnnotationType && !type.eIsProxy()
					&& !annotationLookup.isRepeatable((JvmAnnotationType) type)) {
				for (XAnnotation xAnnotation : sameType) {
					error("Multiple annotations of non-repeatable type @"
							+ xAnnotation.getAnnotationType().getSimpleName()
							+ ". Only annotation types marked @Repeatable can be used multiple times at one target.",
							xAnnotation, XAnnotationsPackage.Literals.XANNOTATION__ANNOTATION_TYPE, INSIGNIFICANT_INDEX, ANNOTATION_MULTIPLE);
				}
			}
		}
	}
}
 
Example 9
Source File: SortACollection.java    From levelup-java-examples with Apache License 2.0 5 votes vote down vote up
/**
 * Display top wrestlers in each weight class
 */
@Test
public void sort_collection_with_multiple_comparables_guava_getfirstElement() {

	// first order elements
	Collections.sort(wrestlers, byWeightClass.compound(byWins.reverse()));

	// next get the first wrestler in each weight class which should have
	// the most wins
	ImmutableListMultimap<Double, Wrestler> wrestlersMappedByWeightClass = Multimaps
			.index(wrestlers, new Function<Wrestler, Double>() {
				public Double apply(Wrestler from) {
					return new Double(from.getWeightClass());
				}
			});

	logger.info(wrestlersMappedByWeightClass);

	// for each weight class get the first element which should be wrestler
	// with most wins
	for (Double weightClass : wrestlersMappedByWeightClass.keySet()) {

		List<Wrestler> weightClassWrestlers = wrestlersMappedByWeightClass
				.get(weightClass);
		logger.info(weightClass + " - "
				+ Iterables.getFirst(weightClassWrestlers, null));
	}
}
 
Example 10
Source File: DiscoveryGenerator.java    From endpoints-java with Apache License 2.0 5 votes vote down vote up
public Result writeDiscovery(
    Iterable<ApiConfig> configs, DiscoveryContext context, SchemaRepository schemaRepository) {
  ImmutableListMultimap<ApiKey, ApiConfig> configsByKey = Multimaps.index(configs,
      new Function<ApiConfig, ApiKey>() {
        @Override public ApiKey apply(ApiConfig config) {
          return config.getApiKey();
        }
      });
  ImmutableMap.Builder<ApiKey, RestDescription> builder = ImmutableMap.builder();
  // "Default" API versions were determined automagically in legacy endpoints.
  // This version only allows to remove an API from default ones by adding
  // defaultVersion = AnnotationBoolean.FALSE to @Api
  ImmutableSet.Builder<ApiKey> preferred = ImmutableSet.builder();
  for (ApiKey apiKey : configsByKey.keySet()) {
    ImmutableList<ApiConfig> apiConfigs = configsByKey.get(apiKey);
    if (context.generateAll || apiConfigs.get(0).getIsDiscoverable()) {
      builder.put(apiKey, writeApi(apiKey, apiConfigs, context, schemaRepository));
      // last config takes precedence (same as writeApi)
      if (Iterables.getLast(apiConfigs).getIsDefaultVersion()) {
        preferred.add(apiKey);
      }
    }
  }
  ImmutableMap<ApiKey, RestDescription> discoveryDocs = builder.build();
  return Result.builder()
      .setDiscoveryDocs(discoveryDocs)
      .setDirectory(generateDirectory(discoveryDocs, preferred.build(), context))
      .build();
}
 
Example 11
Source File: ProductLookupDescriptor.java    From metasfresh-webui-api-legacy with GNU General Public License v3.0 5 votes vote down vote up
@VisibleForTesting
static LookupValuesList explodeLookupValuesByAvailableStockGroups(
		@NonNull final LookupValuesList initialLookupValues,
		@NonNull final List<Group> availableStockGroups,
		final boolean displayATPOnlyIfPositive,
		@NonNull final String adLanguage)
{
	if (initialLookupValues.isEmpty())
	{
		return initialLookupValues;
	}
	if (availableStockGroups.isEmpty())
	{
		return initialLookupValues;
	}

	final ImmutableListMultimap<ProductId, Group> groupsByProductId = Multimaps.index(availableStockGroups, Group::getProductId);
	final ArrayList<ProductWithATP> productWithATPs = new ArrayList<>();

	for (final LookupValue productLookupValue : initialLookupValues)
	{
		final ProductId productId = productLookupValue.getIdAs(ProductId::ofRepoId);
		final ImmutableList<Group> groups = groupsByProductId.get(productId);

		productWithATPs.addAll(createProductWithATPs(productLookupValue, groups, displayATPOnlyIfPositive));
	}

	return productWithATPs.stream()
			.map(productWithATP -> createProductLookupValue(productWithATP, adLanguage))
			.collect(LookupValuesList.collect());
}
 
Example 12
Source File: LinkageCheckTask.java    From cloud-opensource-java with Apache License 2.0 5 votes vote down vote up
private String dependencyPathToArtifacts(
    ResolvedComponentResult componentResult, Set<ClassPathEntry> classPathEntries) {

  ImmutableSet<String> targetCoordinates =
      classPathEntries.stream()
          .map(ClassPathEntry::getArtifact)
          .map(Artifacts::toCoordinates)
          .collect(toImmutableSet());

  StringBuilder output = new StringBuilder();

  ArrayDeque<ResolvedComponentResult> stack = new ArrayDeque<>();
  stack.add(componentResult);

  ImmutableListMultimap.Builder<String, String> coordinatesToDependencyPaths =
      ImmutableListMultimap.builder();

  recordDependencyPaths(coordinatesToDependencyPaths, stack, targetCoordinates);

  ImmutableListMultimap<String, String> dependencyPaths = coordinatesToDependencyPaths.build();
  for (String coordinates : dependencyPaths.keySet()) {
    output.append(coordinates + " is at:\n");
    for (String dependencyPath : dependencyPaths.get(coordinates)) {
      output.append("  " + dependencyPath + "\n");
    }
  }

  return output.toString();
}
 
Example 13
Source File: TerminalInterface.java    From size-analyzer with Apache License 2.0 4 votes vote down vote up
public void displaySuggestions() {
  ImmutableListMultimap<Category, Suggestion> categorizedSuggestions = categorizeSuggestions();

  if (categorizedSuggestions.size() == 0) {
    System.out.println("No size saving suggestions found.");
    return;
  }
  ImmutableList<Category> categoryDisplayOrder = getCategoryDisplayOrder(categorizedSuggestions);

  Long runningTotal = 0L;
  for (Category category : categoryDisplayOrder) {
    ImmutableList<Suggestion> suggestions = categorizedSuggestions.get(category);
    Long totalSavings = getBytesSavedForSuggestionList(suggestions);
    System.out.println(
        Ansi.ansi()
            .fg(Color.GREEN)
            .a(CATEGORY_TO_STRING.get(category))
            .fg(Color.RED)
            .a(humanReadableByteCount(totalSavings))
            .reset());
    if (showFixes) {
      applyFixesInteractively(suggestions, category);
    } else {
      if (displayDetails) {
        suggestions.forEach(TerminalInterface::prettyPrintSuggestion);
      }
      if (applyFixes) {
        System.out.println("applying all available fixes for category: " + category);
        suggestions.stream()
            .filter(suggestion -> suggestion.getAutoFix() != null)
            .map(Suggestion::getAutoFix)
            .forEach(AutoFix::apply);
      }
    }
    runningTotal += totalSavings;
  }

  // Print out total size savings suggested.
  System.out.println(
      Ansi.ansi()
          .fg(Color.GREEN)
          .a("Total size savings of ")
          .fg(Color.RED)
          .a(humanReadableByteCount(runningTotal))
          .reset()
          .a(" found.")
          .reset());
  // if we are only showing suggestions, let them know of other available options.
  if (!applyFixes && !showFixes) {
    if (!displayDetails) {
      System.out.println(
          "The -d flag will display a list of individual suggestions for each category.");
    }
    // if there are any suggestions with a fix, explicitly let developers know they can apply them
    // or be shown them.
    if (this.suggestions.stream().anyMatch(suggestion -> suggestion.getAutoFix() != null)) {
      System.out.println(
          "The --apply-all flag will automatically apply any available fixes while"
              + " the --show-fixes flag allows for fixes to be selectively applied.");
    }
  }
}
 
Example 14
Source File: JournalSet.java    From big-c with Apache License 2.0 4 votes vote down vote up
/**
 * Return a manifest of what finalized edit logs are available. All available
 * edit logs are returned starting from the transaction id passed. If
 * 'fromTxId' falls in the middle of a log, that log is returned as well.
 * 
 * @param fromTxId Starting transaction id to read the logs.
 * @return RemoteEditLogManifest object.
 */
public synchronized RemoteEditLogManifest getEditLogManifest(long fromTxId) {
  // Collect RemoteEditLogs available from each FileJournalManager
  List<RemoteEditLog> allLogs = Lists.newArrayList();
  for (JournalAndStream j : journals) {
    if (j.getManager() instanceof FileJournalManager) {
      FileJournalManager fjm = (FileJournalManager)j.getManager();
      try {
        allLogs.addAll(fjm.getRemoteEditLogs(fromTxId, false));
      } catch (Throwable t) {
        LOG.warn("Cannot list edit logs in " + fjm, t);
      }
    }
  }
  
  // Group logs by their starting txid
  ImmutableListMultimap<Long, RemoteEditLog> logsByStartTxId =
    Multimaps.index(allLogs, RemoteEditLog.GET_START_TXID);
  long curStartTxId = fromTxId;

  List<RemoteEditLog> logs = Lists.newArrayList();
  while (true) {
    ImmutableList<RemoteEditLog> logGroup = logsByStartTxId.get(curStartTxId);
    if (logGroup.isEmpty()) {
      // we have a gap in logs - for example because we recovered some old
      // storage directory with ancient logs. Clear out any logs we've
      // accumulated so far, and then skip to the next segment of logs
      // after the gap.
      SortedSet<Long> startTxIds = Sets.newTreeSet(logsByStartTxId.keySet());
      startTxIds = startTxIds.tailSet(curStartTxId);
      if (startTxIds.isEmpty()) {
        break;
      } else {
        if (LOG.isDebugEnabled()) {
          LOG.debug("Found gap in logs at " + curStartTxId + ": " +
              "not returning previous logs in manifest.");
        }
        logs.clear();
        curStartTxId = startTxIds.first();
        continue;
      }
    }

    // Find the one that extends the farthest forward
    RemoteEditLog bestLog = Collections.max(logGroup);
    logs.add(bestLog);
    // And then start looking from after that point
    curStartTxId = bestLog.getEndTxId() + 1;
  }
  RemoteEditLogManifest ret = new RemoteEditLogManifest(logs);
  
  if (LOG.isDebugEnabled()) {
    LOG.debug("Generated manifest for logs since " + fromTxId + ":"
        + ret);      
  }
  return ret;
}
 
Example 15
Source File: MultiArchBinarySupport.java    From bazel with Apache License 2.0 4 votes vote down vote up
/**
 * Returns a set of {@link DependencySpecificConfiguration} instances that comprise all
 * information about the dependencies for each child configuration. This can be used both to
 * register actions in {@link #registerActions} and collect provider information to be propagated
 * upstream.
 *
 * @param childConfigurationsAndToolchains the set of configurations and toolchains for which
 *     dependencies of the current rule are built
 * @param cpuToDepsCollectionMap a map from child configuration CPU to providers that "deps" of
 *     the current rule have propagated in that configuration
 * @param dylibProviders {@link TransitiveInfoCollection}s that dynamic library dependencies of
 *     the current rule have propagated
 * @throws RuleErrorException if there are attribute errors in the current rule context
 */
public ImmutableSet<DependencySpecificConfiguration> getDependencySpecificConfigurations(
    Map<BuildConfiguration, CcToolchainProvider> childConfigurationsAndToolchains,
    ImmutableListMultimap<String, TransitiveInfoCollection> cpuToDepsCollectionMap,
    ImmutableListMultimap<String, ConfiguredTargetAndData> cpuToCTATDepsCollectionMap,
    ImmutableList<TransitiveInfoCollection> dylibProviders)
    throws RuleErrorException, InterruptedException {
  Iterable<ObjcProvider> dylibObjcProviders = getDylibObjcProviders(dylibProviders);
  Iterable<ObjcProtoProvider> dylibProtoProviders =
      getTypedProviders(dylibProviders, ObjcProtoProvider.STARLARK_CONSTRUCTOR);
  NestedSet<Artifact> protosToAvoid = protoArtifactsToAvoid(dylibProtoProviders);
  ImmutableSet.Builder<DependencySpecificConfiguration> childInfoBuilder = ImmutableSet.builder();

  for (BuildConfiguration childToolchainConfig : childConfigurationsAndToolchains.keySet()) {
    String childCpu = childToolchainConfig.getCpu();
    Iterable<TransitiveInfoCollection> infoCollections = cpuToDepsCollectionMap.get(childCpu);
    ImmutableList<ObjcProtoProvider> depProtoProviders =
        getTypedProviders(infoCollections, ObjcProtoProvider.STARLARK_CONSTRUCTOR);
    Optional<ObjcProvider> protosObjcProvider;
    if (ObjcRuleClasses.objcConfiguration(ruleContext).enableAppleBinaryNativeProtos()) {
      ProtobufSupport protoSupport =
          new ProtobufSupport(
                  ruleContext,
                  childToolchainConfig,
                  protosToAvoid,
                  depProtoProviders,
                  ProtobufSupport.getTransitivePortableProtoFilters(depProtoProviders),
                  childConfigurationsAndToolchains.get(childToolchainConfig))
              .registerGenerationAction()
              .registerCompilationAction();
      protosObjcProvider = protoSupport.getObjcProvider();
    } else {
      protosObjcProvider = Optional.absent();
    }

    IntermediateArtifacts intermediateArtifacts =
        ObjcRuleClasses.intermediateArtifacts(ruleContext, childToolchainConfig);

    Iterable<ObjcProvider> additionalDepProviders =
        Iterables.concat(
            dylibObjcProviders,
            protosObjcProvider.asSet());

    ObjcCommon common =
        common(
            ruleContext,
            childToolchainConfig,
            intermediateArtifacts,
            nullToEmptyList(cpuToCTATDepsCollectionMap.get(childCpu)),
            additionalDepProviders);
    ObjcProvider objcProviderWithDylibSymbols = common.getObjcProviderBuilder().build();
    ObjcProvider objcProvider =
        objcProviderWithDylibSymbols.subtractSubtrees(dylibObjcProviders, ImmutableList.of());

    childInfoBuilder.add(
        DependencySpecificConfiguration.create(
            childToolchainConfig,
            childConfigurationsAndToolchains.get(childToolchainConfig),
            objcProvider,
            objcProviderWithDylibSymbols));
  }

  return childInfoBuilder.build();
}
 
Example 16
Source File: LegalEntityRatesCurvesCsvLoaderTest.java    From Strata with Apache License 2.0 4 votes vote down vote up
@Test
public void test_loadAllDates() {
  LocalDate sampleDate = ALL_DATES.get(3); // 2017-04-21
  ImmutableList<LocalDate> expDates = ImmutableList.of(
      LocalDate.of(2017, 7, 21), LocalDate.of(2017, 10, 7), LocalDate.of(2018, 4, 13), LocalDate.of(2019, 4, 12),
      LocalDate.of(2020, 3, 20), LocalDate.of(2021, 3, 19), LocalDate.of(2022, 3, 19), LocalDate.of(2023, 3, 17),
      LocalDate.of(2024, 6, 17), LocalDate.of(2025, 3, 18), LocalDate.of(2026, 3, 20), LocalDate.of(2027, 3, 20),
      LocalDate.of(2031, 12, 19), LocalDate.of(2037, 3, 17), LocalDate.of(2047, 3, 17), LocalDate.of(2056, 3, 17));
  ImmutableList<String> expTenors = ImmutableList.of(
      "3M", "6M", "1Y", "2Y", "3Y", "4Y", "5Y", "6Y", "7Y", "8Y", "9Y", "10Y", "15Y", "20Y", "30Y", "40Y");
  RepoGroup repoGroup = RepoGroup.of("JP-REPO");
  DoubleArray expRepoXValues = DoubleArray.of(3, n -> ACT_365F.relativeYearFraction(sampleDate, expDates.get(n)));
  DoubleArray expRepoYValues = DoubleArray.of(-0.0019521, -0.0016021, -0.0022521);
  ImmutableList<LabelDateParameterMetadata> expRepoMetadata = IntStream.range(0, 3)
      .mapToObj(n -> LabelDateParameterMetadata.of(expDates.get(n), expTenors.get(n)))
      .collect(Guavate.toImmutableList());
  LegalEntityGroup legalEntityGroup = LegalEntityGroup.of("JP-GOVT");
  DoubleArray expIssuerXValues =
      DoubleArray.of(expDates.size(), n -> ACT_365F.relativeYearFraction(sampleDate, expDates.get(n)));
  DoubleArray expIssuerYValues = DoubleArray.of(
      -0.0019511690511744527, -0.001497422302092893, -0.0021798583657932176, -0.002215700360912938, -0.0021722324679574866,
      -0.001922059591219172, -0.0015461646763548528, -0.0014835851245462084, -0.001118669580570464, -5.476767138782941E-4,
      -2.2155596172855965E-4, 2.0333291172821893E-5, 0.00284500423293463, 0.005876533417933958, 0.007957581583531789,
      0.009134630405512047);
  ImmutableList<LabelDateParameterMetadata> expIssuerMetadata = IntStream.range(0, expDates.size())
      .mapToObj(n -> LabelDateParameterMetadata.of(expDates.get(n), expTenors.get(n)))
      .collect(Guavate.toImmutableList());

  ImmutableListMultimap<LocalDate, LegalEntityCurveGroup> allCurves = LegalEntityRatesCurvesCsvLoader.loadAllDates(
      ResourceLocator.of(GROUPS),
      ResourceLocator.of(SETTINGS),
      ImmutableList.of(ResourceLocator.of(CURVES_1), ResourceLocator.of(CURVES_2)));
  assertThat(allCurves.keySet().containsAll(ALL_DATES)).isTrue();
  ImmutableList<LegalEntityCurveGroup> groups = allCurves.get(sampleDate);
  assertThat(groups).hasSize(2);
  // group 0
  LegalEntityCurveGroup group0 = groups.get(0);
  assertThat(group0.getName()).isEqualTo(CurveGroupName.of("Default1"));
  // repo
  assertThat(group0.getRepoCurves()).hasSize(1);
  Curve repoCurve = group0.getRepoCurves().get(Pair.of(repoGroup, JPY));
  InterpolatedNodalCurve expectedRepoCurve = InterpolatedNodalCurve.of(
      Curves.zeroRates(CurveName.of("JP-REPO-1"), ACT_365F, expRepoMetadata),
      expRepoXValues, expRepoYValues, CurveInterpolators.LINEAR, CurveExtrapolators.FLAT, CurveExtrapolators.FLAT);
  assertThat(repoCurve).isEqualTo(expectedRepoCurve);
  // issuer
  assertThat(group0.getIssuerCurves()).hasSize(2);
  Curve issuerCurve = group0.getIssuerCurves().get(Pair.of(legalEntityGroup, JPY));
  InterpolatedNodalCurve expectedIssuerCurve = InterpolatedNodalCurve.of(
      Curves.zeroRates(CurveName.of("JP-GOVT-1"), ACT_365F, expIssuerMetadata),
      expIssuerXValues, expIssuerYValues, CurveInterpolators.LINEAR, CurveExtrapolators.FLAT, CurveExtrapolators.FLAT);
  assertThat(issuerCurve).isEqualTo(expectedIssuerCurve);
  Curve usIssuerCurve = group0.getIssuerCurves().get(Pair.of(LegalEntityGroup.of("US-GOVT"), USD));
  expectedIssuerCurve = InterpolatedNodalCurve.of(
      Curves.zeroRates(CurveName.of("US-GOVT"), ACT_360, expIssuerMetadata),
      DoubleArray.of(expDates.size(), n -> ACT_360.relativeYearFraction(sampleDate, expDates.get(n))),
      expIssuerYValues, CurveInterpolators.NATURAL_SPLINE, CurveExtrapolators.FLAT, CurveExtrapolators.FLAT);
  assertThat(usIssuerCurve).isEqualTo(expectedIssuerCurve);
  // group 1
  LegalEntityCurveGroup group1 = groups.get(1);
  assertThat(group1.getName()).isEqualTo(CurveGroupName.of("Default2"));
  // repo
  repoCurve = group1.getRepoCurves().get(Pair.of(repoGroup, JPY));
  expectedRepoCurve = InterpolatedNodalCurve.of(
      Curves.zeroRates(CurveName.of("JP-REPO-2"), ACT_365F, expRepoMetadata),
      expRepoXValues, expRepoYValues, CurveInterpolators.DOUBLE_QUADRATIC, CurveExtrapolators.LINEAR,
      CurveExtrapolators.LINEAR);
  assertThat(repoCurve).isEqualTo(expectedRepoCurve);
  // issuer
  assertThat(group1.getIssuerCurves()).hasSize(1);
  issuerCurve = group1.getIssuerCurves().get(Pair.of(legalEntityGroup, JPY));
  expectedIssuerCurve = InterpolatedNodalCurve.of(
      Curves.zeroRates(CurveName.of("JP-GOVT-2"), ACT_365F, expIssuerMetadata),
      expIssuerXValues, expIssuerYValues, CurveInterpolators.DOUBLE_QUADRATIC, CurveExtrapolators.LINEAR,
      CurveExtrapolators.LINEAR);
  assertThat(issuerCurve).isEqualTo(expectedIssuerCurve);
}
 
Example 17
Source File: JournalSet.java    From hadoop with Apache License 2.0 4 votes vote down vote up
/**
 * Return a manifest of what finalized edit logs are available. All available
 * edit logs are returned starting from the transaction id passed. If
 * 'fromTxId' falls in the middle of a log, that log is returned as well.
 * 
 * @param fromTxId Starting transaction id to read the logs.
 * @return RemoteEditLogManifest object.
 */
public synchronized RemoteEditLogManifest getEditLogManifest(long fromTxId) {
  // Collect RemoteEditLogs available from each FileJournalManager
  List<RemoteEditLog> allLogs = Lists.newArrayList();
  for (JournalAndStream j : journals) {
    if (j.getManager() instanceof FileJournalManager) {
      FileJournalManager fjm = (FileJournalManager)j.getManager();
      try {
        allLogs.addAll(fjm.getRemoteEditLogs(fromTxId, false));
      } catch (Throwable t) {
        LOG.warn("Cannot list edit logs in " + fjm, t);
      }
    }
  }
  
  // Group logs by their starting txid
  ImmutableListMultimap<Long, RemoteEditLog> logsByStartTxId =
    Multimaps.index(allLogs, RemoteEditLog.GET_START_TXID);
  long curStartTxId = fromTxId;

  List<RemoteEditLog> logs = Lists.newArrayList();
  while (true) {
    ImmutableList<RemoteEditLog> logGroup = logsByStartTxId.get(curStartTxId);
    if (logGroup.isEmpty()) {
      // we have a gap in logs - for example because we recovered some old
      // storage directory with ancient logs. Clear out any logs we've
      // accumulated so far, and then skip to the next segment of logs
      // after the gap.
      SortedSet<Long> startTxIds = Sets.newTreeSet(logsByStartTxId.keySet());
      startTxIds = startTxIds.tailSet(curStartTxId);
      if (startTxIds.isEmpty()) {
        break;
      } else {
        if (LOG.isDebugEnabled()) {
          LOG.debug("Found gap in logs at " + curStartTxId + ": " +
              "not returning previous logs in manifest.");
        }
        logs.clear();
        curStartTxId = startTxIds.first();
        continue;
      }
    }

    // Find the one that extends the farthest forward
    RemoteEditLog bestLog = Collections.max(logGroup);
    logs.add(bestLog);
    // And then start looking from after that point
    curStartTxId = bestLog.getEndTxId() + 1;
  }
  RemoteEditLogManifest ret = new RemoteEditLogManifest(logs);
  
  if (LOG.isDebugEnabled()) {
    LOG.debug("Generated manifest for logs since " + fromTxId + ":"
        + ret);      
  }
  return ret;
}