Java Code Examples for com.google.common.collect.BiMap#inverse()

The following examples show how to use com.google.common.collect.BiMap#inverse() . 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: TodoReplace.java    From copybara with Apache License 2.0 6 votes vote down vote up
@Override
public Transformation reverse() throws NonReversibleValidationException {
  if (mode != Mode.MAP_OR_FAIL && mode != Mode.MAP_OR_IGNORE) {
    throw new NonReversibleValidationException(location, mode + " mode is not reversible");
  }

  BiMap<String, String> mapping;
  try {
    mapping = HashBiMap.create(this.mapping);
  } catch (IllegalArgumentException e) {
    throw new NonReversibleValidationException(location,
        "Non-reversible mapping: " + e.getMessage());
  }

  return new TodoReplace(
      location,
      glob,
      todoTags,
      mode,
      mapping.inverse(),
      defaultString,
      parallelizer,
      regexIgnorelist);
}
 
Example 2
Source File: TimeGraphFindDialog.java    From tracecompass with Eclipse Public License 2.0 6 votes vote down vote up
private int findNext(int startIndex, String findString, BiMap<ITimeGraphEntry, Integer> items, SearchOptions options) {
    FindTarget findTarget = fFindTarget;
    if (findTarget != null) {
        if (findString == null || findString.length() == 0) {
            return -1;
        }

        final @NonNull Pattern pattern = getPattern(findString, options);
        int index = adjustIndex(startIndex, items.size(), options.forwardSearch);
        BiMap<Integer, ITimeGraphEntry> entries = items.inverse();
        while (index >= 0 && index < entries.size()) {
            final @Nullable ITimeGraphEntry entry = entries.get(index);
            if (entry != null) {
                String[] columnTexts = findTarget.getColumnTexts(entry);
                for (int i = 0; i < columnTexts.length; i++) {
                    String columnText = columnTexts[i];
                    if (columnText != null && !columnText.isEmpty() && pattern.matcher(columnTexts[i]).find()) {
                        return index;
                    }
                }
            }
            index = options.forwardSearch ? ++index : --index;
        }
    }
    return -1;
}
 
Example 3
Source File: BiMapExample.java    From levelup-java-examples with Apache License 2.0 6 votes vote down vote up
@Test
public void bidirectional_map_with_guava () {
	
	BiMap<String, String> dialectConverterForWisconsinites = 
			HashBiMap.create();
	
	dialectConverterForWisconsinites.put("bratwurst", "brat");
	dialectConverterForWisconsinites.put("drinking fountain", "bubbler");
	dialectConverterForWisconsinites.put("that", "dat");
	dialectConverterForWisconsinites.put("alright", "iet");
	dialectConverterForWisconsinites.put("soda", "pop");

	BiMap<String, String> dialectConverterForEveryoneElse = dialectConverterForWisconsinites.inverse();

	logger.info(dialectConverterForEveryoneElse);
	
	assertNotNull(dialectConverterForEveryoneElse.get("brat"));
}
 
Example 4
Source File: ReverseMapLookup.java    From levelup-java-examples with Apache License 2.0 6 votes vote down vote up
@Test
public void flip_map_entries_with_guava() {

	BiMap<String, String> stateCodeToDescription = HashBiMap.create();

	stateCodeToDescription.put("WI", "Wisconsin");
	stateCodeToDescription.put("MN", "Minnesota");
	stateCodeToDescription.put("FL", "Florida");
	stateCodeToDescription.put("IA", "Iowa");
	stateCodeToDescription.put("OH", "Ohio");

	BiMap<String, String> descriptionToStateCode = stateCodeToDescription
			.inverse();

	logger.info(descriptionToStateCode);

	assertEquals("WI", descriptionToStateCode.get("Wisconsin"));
}
 
Example 5
Source File: MetaDataPlanContext.java    From fdb-record-layer with Apache License 2.0 5 votes vote down vote up
private MetaDataPlanContext(@Nonnull RecordMetaData metaData, @Nonnull Set<String> recordTypes,
                            @Nonnull RecordStoreState recordStoreState, @Nonnull BiMap<Index, String> indexes,
                            @Nonnull ImmutableSet<IndexEntrySource> indexEntrySources,
                            @Nonnull KeyExpression commonPrimaryKey, int greatestPrimaryKeyWidth) {
    this.metaData = metaData;
    this.recordTypes = recordTypes;
    this.recordStoreState = recordStoreState;
    this.indexes = indexes;
    this.indexesByName = indexes.inverse();
    this.indexEntrySources = indexEntrySources;
    this.commonPrimaryKey = commonPrimaryKey;
    this.greatestPrimaryKeyWidth = greatestPrimaryKeyWidth;
}
 
Example 6
Source File: ReplaceIdGenerators.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
public ObfuscatedNameSuppier(
    RenameStrategy renameStrategy, BiMap<String, String> previousMappings) {
  this.previousMappings = previousMappings.inverse();
  this.generator =
      new NameGenerator(previousMappings.keySet(), "", null);
  this.renameStrategy = renameStrategy;
}
 
Example 7
Source File: EntityUtil.java    From jpmml-evaluator with GNU Affero General Public License v3.0 5 votes vote down vote up
static
public <E extends Entity<?>> String getId(E entity, BiMap<String, E> entityRegistry){
	Object id = entity.getId();

	if(id == null){
		BiMap<E, String> inversedEntityRegistry = entityRegistry.inverse();

		return inversedEntityRegistry.get(entity);
	}

	return TypeUtil.format(id);
}
 
Example 8
Source File: ServerTableSizeReader.java    From incubator-pinot with Apache License 2.0 4 votes vote down vote up
public Map<String, List<SegmentSizeInfo>> getSegmentSizeInfoFromServers(BiMap<String, String> serverEndPoints,
    String tableNameWithType, int timeoutMs) {
  int numServers = serverEndPoints.size();
  LOGGER.info("Reading segment sizes from {} servers for table: {} with timeout: {}ms", numServers, tableNameWithType,
      timeoutMs);

  List<String> serverUrls = new ArrayList<>(numServers);
  BiMap<String, String> endpointsToServers = serverEndPoints.inverse();
  for (String endpoint : endpointsToServers.keySet()) {
    String tableSizeUri = "http://" + endpoint + "/table/" + tableNameWithType + "/size";
    serverUrls.add(tableSizeUri);
  }

  // TODO: use some service other than completion service so that we know which server encounters the error
  CompletionService<GetMethod> completionService =
      new MultiGetRequest(_executor, _connectionManager).execute(serverUrls, timeoutMs);
  Map<String, List<SegmentSizeInfo>> serverToSegmentSizeInfoListMap = new HashMap<>();

  for (int i = 0; i < numServers; i++) {
    GetMethod getMethod = null;
    try {
      getMethod = completionService.take().get();
      URI uri = getMethod.getURI();
      String instance = endpointsToServers.get(uri.getHost() + ":" + uri.getPort());
      if (getMethod.getStatusCode() >= 300) {
        LOGGER.error("Server: {} returned error: {}", instance, getMethod.getStatusCode());
        continue;
      }
      TableSizeInfo tableSizeInfo =
          JsonUtils.inputStreamToObject(getMethod.getResponseBodyAsStream(), TableSizeInfo.class);
      serverToSegmentSizeInfoListMap.put(instance, tableSizeInfo.segments);
    } catch (Exception e) {
      // Ignore individual exceptions because the exception has been logged in MultiGetRequest
      // Log the number of failed servers after gathering all responses
    } finally {
      if (getMethod != null) {
        getMethod.releaseConnection();
      }
    }
  }

  int numServersResponded = serverToSegmentSizeInfoListMap.size();
  if (numServersResponded != numServers) {
    LOGGER.warn("Finish reading segment sizes for table: {} with {}/{} servers responded", tableNameWithType,
        numServersResponded, numServers);
  } else {
    LOGGER.info("Finish reading segment sizes for table: {}", tableNameWithType);
  }
  return serverToSegmentSizeInfoListMap;
}