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

The following examples show how to use com.google.common.collect.BiMap#containsValue() . 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: HBaseStoreManager.java    From atlas with Apache License 2.0 6 votes vote down vote up
public static String shortenCfName(BiMap<String, String> shortCfNameMap, String longName) throws PermanentBackendException {
    final String s;
    if (shortCfNameMap.containsKey(longName)) {
        s = shortCfNameMap.get(longName);
        Preconditions.checkNotNull(s);
        logger.debug("Substituted default CF name \"{}\" with short form \"{}\" to reduce HBase KeyValue size", longName, s);
    } else {
        if (shortCfNameMap.containsValue(longName)) {
            String fmt = "Must use CF long-form name \"%s\" instead of the short-form name \"%s\" when configured with %s=true";
            String msg = String.format(fmt, shortCfNameMap.inverse().get(longName), longName, SHORT_CF_NAMES.getName());
            throw new PermanentBackendException(msg);
        }
        s = longName;
        logger.debug("Kept default CF name \"{}\" because it has no associated short form", s);
    }
    return s;
}
 
Example 2
Source File: DefaultValidationNotificationService.java    From dhis2-core with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
private static Map<Set<User>, SortedSet<MessagePair>> groupRecipientsForMessagePairs(
    Map<User, SortedSet<MessagePair>> messagePairsPerUser )
{
    BiMap<Set<User>, SortedSet<MessagePair>> grouped = HashBiMap.create();

    for ( Map.Entry<User, SortedSet<MessagePair>> entry : messagePairsPerUser.entrySet() )
    {
        User user = entry.getKey();
        SortedSet<MessagePair> setOfPairs = entry.getValue();

        if ( grouped.containsValue( setOfPairs ) )
        {
            // Value exists -> Add user to the existing key set
            grouped.inverse().get( setOfPairs ).add( user );
        }
        else
        {
            // Value doesn't exist -> Add the [user, set] as a new entry
            grouped.put( Sets.newHashSet( user ), setOfPairs );
        }
    }

    return grouped;
}
 
Example 3
Source File: IfcModel.java    From BIMserver with GNU Affero General Public License v3.0 6 votes vote down vote up
@SuppressWarnings("rawtypes")
private void fixOids(IdEObject idEObject, OidProvider oidProvider, BiMap<Long, IdEObject> temp) {
	if (idEObject == null) {
		return;
	}
	if (temp.containsValue(idEObject)) {
		return;
	}
	((IdEObjectImpl) idEObject).setOid(oidProvider.newOid(idEObject.eClass()));
	if (objects.containsValue(idEObject)) {
		temp.put(idEObject.getOid(), idEObject);
	}
	for (EReference eReference : idEObject.eClass().getEAllReferences()) {
		Object val = idEObject.eGet(eReference);
		if (eReference.isMany()) {
			List list = (List) val;
			for (Object o : list) {
				fixOids((IdEObject) o, oidProvider, temp);
			}
		} else {
			fixOids((IdEObject) val, oidProvider, temp);
		}
	}
}
 
Example 4
Source File: ManagementServiceImpl.java    From usergrid with Apache License 2.0 6 votes vote down vote up
@Override
public List<OrganizationInfo> getOrganizations( UUID startResult, int count ) throws Exception {
    // still need the bimap to search for existing
    BiMap<UUID, String> organizations = HashBiMap.create();
    EntityManager em = emf.getEntityManager(smf.getManagementAppId());
    Results results = em.getCollection(em.getApplicationRef(),
        Schema.COLLECTION_GROUPS, startResult, count, Level.ALL_PROPERTIES, false);
    List<OrganizationInfo> orgs = new ArrayList<>( results.size() );
    OrganizationInfo orgInfo;
    for ( Entity entity : results.getEntities() ) {

        String path = ( String ) entity.getProperty( PROPERTY_PATH );

        if ( organizations.containsValue( path ) ) {
            path += "DUPLICATE";
        }
        orgInfo = new OrganizationInfo( entity.getUuid(), path );
        orgs.add( orgInfo );
        organizations.put( entity.getUuid(), path );
    }
    return orgs;
}
 
Example 5
Source File: UniqueCachedNaming.java    From immutables with Apache License 2.0 6 votes vote down vote up
private static <T> BiMap<T, String> buildBiMap(Iterable<T> values) {
  @SuppressWarnings("unchecked")
  Suggester<T> suggester = (Suggester<T>) PREFIX_SUGGESTER;
  final BiMap<T, String> map = HashBiMap.create();
  for (T value: values) {
    if (!map.containsKey(value)) {
      String name;
      for (int i = 0; ; i++) {
        name = suggester.suggest(value, i, map.size());
        if (!map.containsValue(name)) {
          map.put(value, name);
          break; // name found, break the loop
        }
      }
    }
  }

  return ImmutableBiMap.copyOf(map);
}
 
Example 6
Source File: KeywordHelper.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
private BiMap<CharSequence, String> createKeywordMap(Grammar grammar) {
	List<ParserRule> parserRules = GrammarUtil.allParserRules(grammar);
	List<EnumRule> enumRules = GrammarUtil.allEnumRules(grammar);
	Iterator<EObject> iter = Iterators.concat(
			EcoreUtil.<EObject>getAllContents(parserRules), EcoreUtil.<EObject>getAllContents(enumRules));
	Iterator<Keyword> filtered = Iterators.filter(iter, Keyword.class);
	Iterator<String> transformed = Iterators.transform(filtered, new Function<Keyword, String>() {
		@Override
		public String apply(Keyword from) {
			return from.getValue();
		}
	});
	TreeSet<String> treeSet = Sets.newTreeSet(new Comparator<String>() {
		@Override
		public int compare(String o1, String o2) {
			if (o1.length() == o2.length())
				return o1.compareTo(o2);
			return Integer.valueOf(o1.length()).compareTo(Integer.valueOf(o2.length()));
		}
	});
	Iterators.addAll(treeSet, transformed);
	BiMap<CharSequence, String> result = HashBiMap.create();
	for(String s: treeSet) {
		CharSequence key = createKey(s);
		String readableName = toAntlrTokenIdentifier(s);
		if (result.containsValue(readableName)) {
			int i = 1;
			String next = readableName + "_" + i;
			while(result.containsValue(next)) {
				i++;
				next = readableName + "_" + i;
			}
			readableName = next;
		}
		result.put(key, readableName);
	}
	return result;
}
 
Example 7
Source File: TaskSchedule.java    From mars-sim with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Gets the ID of a BiMap
 * 
 * @param map
 * @param value
 * @return
 */
public int getID(BiMap<Integer, String> map, String value) {
	if (map.containsValue(value)) {
		return map.inverse().get(value);
	} else {
		int size = map.size();
		map.put(size + 1, value);
		return size + 1;
	}
}
 
Example 8
Source File: IfcModel.java    From BIMserver with GNU Affero General Public License v3.0 5 votes vote down vote up
private void fixOidsFlat(IdEObject idEObject, OidProvider oidProvider, BiMap<Long, IdEObject> temp) {
	if (idEObject == null) {
		return;
	}
	if (temp.containsValue(idEObject)) {
		return;
	}
	((IdEObjectImpl) idEObject).setOid(oidProvider.newOid(idEObject.eClass()));
	if (objects.containsValue(idEObject)) {
		temp.put(idEObject.getOid(), idEObject);
	}
}
 
Example 9
Source File: IfcModel.java    From BIMserver with GNU Affero General Public License v3.0 5 votes vote down vote up
@SuppressWarnings("rawtypes")
private void checkDoubleOidsPlusReferences(BiMap<IdEObject, Long> done, IdEObject idEObject) {
	if (idEObject == null) {
		return;
	}
	if (idEObject.eClass().getEAnnotation("wrapped") != null) {
		return;
	}
	if (done.containsKey(idEObject)) {
		return;
	}
	if (done.containsValue(idEObject.getOid())) {
		showBackReferences(idEObject);
		IdEObject existing = done.inverse().get(idEObject.getOid());
		showBackReferences(existing);
		throw new RuntimeException("Double oid: " + idEObject.getOid() + " " + idEObject + ", " + existing);
	}
	done.put(idEObject, idEObject.getOid());
	for (EReference eReference : idEObject.eClass().getEAllReferences()) {
		if (eReference.isMany()) {
			List list = (List) idEObject.eGet(eReference);
			for (Object o : list) {
				checkDoubleOidsPlusReferences(done, (IdEObject) o);
			}
		} else {
			checkDoubleOidsPlusReferences(done, (IdEObject) idEObject.eGet(eReference));
		}
	}
}
 
Example 10
Source File: MetaDataEvolutionValidator.java    From fdb-record-layer with Apache License 2.0 4 votes vote down vote up
/**
 * Validate that the record types have all been evolved in a legal way. In particular, this makes sure that
 * each record type defined in the union descriptor is in the new union descriptor in the correct
 * place. It will then verify that each message type has been updated in a legal way, i.e., that it only
 * includes new fields.
 *
 * @param oldUnionDescriptor the union descriptor for the existing meta-data for some record store
 * @param newUnionDescriptor the new proposed meta-data
 */
@SuppressWarnings("PMD.CompareObjectsWithEquals")
public void validateUnion(@Nonnull Descriptor oldUnionDescriptor, @Nonnull Descriptor newUnionDescriptor) {
    if (oldUnionDescriptor == newUnionDescriptor) {
        // Don't bother validating the record types if they are all the same.
        return;
    }
    final BiMap<Descriptor, Descriptor> updatedDescriptors = HashBiMap.create(oldUnionDescriptor.getFields().size());
    final Set<Pair<Descriptor, Descriptor>> seenDescriptors = new HashSet<>();

    for (FieldDescriptor oldUnionField : oldUnionDescriptor.getFields()) {
        if (!oldUnionField.getType().equals(FieldDescriptor.Type.MESSAGE)) {
            throw new MetaDataException("field in union is not a message type", LogMessageKeys.FIELD_NAME, oldUnionField.getName());
        }
        int fieldNumber = oldUnionField.getNumber();
        FieldDescriptor newUnionField = newUnionDescriptor.findFieldByNumber(fieldNumber);
        if (newUnionField != null) {
            if (!newUnionField.getType().equals(FieldDescriptor.Type.MESSAGE)) {
                throw new MetaDataException("field in new union is not a message type", LogMessageKeys.FIELD_NAME, newUnionField.getName());
            }
            Descriptor oldRecord = oldUnionField.getMessageType();
            Descriptor newRecord = newUnionField.getMessageType();

            // Verify that all fields of the same type in the old union are also of the same type
            // in the new union (i.e., that there are no "splits" or "merges" of record types).
            Descriptor alreadySeenNewRecord = updatedDescriptors.get(oldRecord);
            if (alreadySeenNewRecord != null) {
                if (alreadySeenNewRecord != newRecord) {
                    // A "split" -- the same type in the old union points to two different types in the new union
                    throw new MetaDataException("record type corresponds to multiple types in new meta-data",
                            LogMessageKeys.OLD_RECORD_TYPE, oldRecord.getName(),
                            LogMessageKeys.NEW_RECORD_TYPE, newRecord.getName() + " & " + alreadySeenNewRecord.getName());
                }
            } else {
                if (updatedDescriptors.containsValue(newRecord)) {
                    // A "merge" -- two different types in the old union point to the same type in the new union
                    final Descriptor alreadySeenOldRecord = updatedDescriptors.inverse().get(newRecord);
                    throw new MetaDataException("record type corresponds to multiple types in old meta-data",
                            LogMessageKeys.OLD_RECORD_TYPE, oldRecord.getName() + " & " + alreadySeenOldRecord.getName(),
                            LogMessageKeys.NEW_RECORD_TYPE, newRecord.getName());
                }
            }
            updatedDescriptors.put(oldRecord, newRecord);

            // Validate the form of the old and new record types
            validateMessage(oldRecord, newRecord, seenDescriptors);
        } else {
            throw new MetaDataException("record type removed from union", LogMessageKeys.RECORD_TYPE, oldUnionField.getMessageType());
        }
    }
}