List of usage examples for java.util Collection getClass
@HotSpotIntrinsicCandidate public final native Class<?> getClass();
From source file:org.springframework.data.neo4j.support.NodeEntityRelationshipTest.java
@Test @Transactional// w ww. j a va 2 s. c o m public void testAddToOneToManyRelationship() { Person michael = persistedPerson("Michael", 35); Person david = persistedPerson("David", 25); Group group = new Group().persist(); group.setPersons(new HashSet<Person>()); group.getPersons().add(michael); group.getPersons().add(david); Collection<Person> personsFromGet = group.getPersons(); assertEquals(new HashSet<Person>(Arrays.asList(david, michael)), personsFromGet); Assert.assertTrue(Set.class.isAssignableFrom(personsFromGet.getClass())); }
From source file:org.springframework.data.neo4j.aspects.support.NodeEntityRelationshipTest.java
@Test @Transactional//from ww w.j a v a 2s . c o m public void testAddToOneToManyRelationship() { Person michael = persistedPerson("Michael", 35); Person david = persistedPerson("David", 25); Group group = new Group().persist(); group.setPersons(new HashSet<Person>()); group.getPersons().add(michael); group.getPersons().add(david); Collection<Person> personsFromGet = group.getPersons(); assertEquals(new HashSet<Person>(Arrays.asList(david, michael)), personsFromGet); assertTrue(Set.class.isAssignableFrom(personsFromGet.getClass())); }
From source file:ome.util.ModelMapper.java
public Collection findCollection(Collection source) { if (source == null) { return null; }/* ww w . j a v a 2s. c o m*/ Collection target = (Collection) model2target.get(source); if (null == target) { if (Set.class.isAssignableFrom(source.getClass())) { target = new HashSet(); } else if (List.class.isAssignableFrom(source.getClass())) { target = new ArrayList(); } else { throw new RuntimeException("Unknown collection type: " + source.getClass()); } model2target.put(source, target); } return target; }
From source file:org.vulpe.controller.vraptor.VulpeVRaptorController.java
/** * Method to add detail.//from ww w . j a v a 2 s. c om * * @param collection * @since 1.0 * @throws OgnlException */ protected void doAddDetail(final Collection collection) throws OgnlException { final Map context = null; final PropertyAccessor accessor = OgnlRuntime.getPropertyAccessor(collection.getClass()); final Integer index = Integer.valueOf(collection.size()); final ENTITY detail = (ENTITY) accessor.getProperty(context, collection, index); vulpe.updateAuditInfo(detail); final ENTITY preparedDetail = prepareDetail(detail); if (!preparedDetail.equals(detail)) { accessor.setProperty(context, collection, index, preparedDetail); } }
From source file:org.apache.ranger.authorization.hbase.ColumnIterator.java
@SuppressWarnings("unchecked") public ColumnIterator(Collection<?> columnCollection) { if (columnCollection != null) { if (columnCollection instanceof Set) { _setIterator = ((Set<byte[]>) columnCollection).iterator(); } else if (columnCollection instanceof List) { _listIterator = ((List<KeyValue>) columnCollection).iterator(); } else { // unexpected // TODO make message better LOG.error("Unexpected type " + columnCollection.getClass().getName() + " passed as value in column family collection"); }/*w w w. j a va 2 s. com*/ } }
From source file:org.apache.openjpa.datacache.QueryKey.java
/** * Convert parameters to a form that is cacheable. Mutable params * will be cloned./* w w w . j a va2 s. c o m*/ */ private static boolean setParams(QueryKey key, StoreContext ctx, Map<Object, Object> params) { if (params == null || params.isEmpty()) return true; Object v; for (Map.Entry<Object, Object> e : params.entrySet()) { v = e.getValue(); if (ImplHelper.isManageable(v)) { if (!ctx.isPersistent(v) || ctx.isNew(v) || ctx.isDeleted(v)) return false; e.setValue(ctx.getObjectId(v)); } if (v instanceof Collection) { Collection<Object> c = (Collection<Object>) v; boolean contentsAreDates = false; if (c.iterator().hasNext()) { // this assumes that the collection is homogeneous Object o = c.iterator().next(); if (ImplHelper.isManageable(o)) return false; // pcl: 27 Jun 2004: if we grow this logic to // handle other mutable types that are not // known to be cloneable, we will have to add // logic to handle them. This is because we // can't just cast to Cloneable and invoke // clone(), as clone() is a protected method // in Object. if (o instanceof Date) contentsAreDates = true; // if the collection is not a known immutable // type, or if it contains mutable instances, // clone it for good measure. if (contentsAreDates || !s_unmod.contains(c.getClass())) { // copy the collection Collection<Object> copy; if (c instanceof SortedSet) copy = new TreeSet<Object>(); else if (c instanceof Set) copy = new HashSet<Object>(); else copy = new ArrayList<Object>(c.size()); if (contentsAreDates) { // must go through by hand and do the // copy, since Date is mutable. for (Iterator<Object> itr2 = c.iterator(); itr2.hasNext();) copy.add(((Date) itr2.next()).clone()); } else copy.addAll(c); e.setValue(copy); } } } else if (v instanceof Date) e.setValue(((Date) v).clone()); } key._params = params; return true; }
From source file:de.micromata.genome.db.jpa.history.impl.CollectionPropertyConverter.java
@Override public List<HistProp> convert(IEmgr<?> emgr, HistoryMetaInfo historyMetaInfo, Object entity, ColumnMetadata pd) {/* w w w . j a v a2 s. c o m*/ Collection<?> col = (Collection<?>) pd.getGetter().get(entity); if (col == null) { return Collections.emptyList(); } EntityMetadata targetEntity = pd.getTargetEntity(); if (col.isEmpty() == true) { HistProp hp = new HistProp(); hp.setName(""); if (targetEntity != null) { hp.setType(targetEntity.getJavaType().getName()); } else { hp.setType(col.getClass().getName()); } hp.setValue(""); return Collections.singletonList(hp); } if (targetEntity != null) { List<Object> pks = new ArrayList<>(); for (Object ob : col) { Object pk = targetEntity.getIdColumn().getGetter().get(ob); if (pk == null) { LOG.warn("Unsaved entity in history"); return Collections.emptyList(); } pks.add(pk); } String sval = StringUtils.join(pks, ','); HistProp hp = new HistProp(); hp.setName(""); hp.setType(targetEntity.getJavaType().getName()); hp.setValue(sval); return Collections.singletonList(hp); } Map<Long, Class<?>> pkSet = new TreeMap<>(); int idx = 0; for (Object ob : col) { if ((ob instanceof DbRecord) == false) { LOG.warn("Cannot create collection history on non DbRecord: " + entity.getClass().getName() + "." + pd.getName() + "[" + idx + "]" + ob.getClass().getName()); continue; } DbRecord<?> rec = (DbRecord<?>) ob; Long lp = rec.getPkAsLong(); if (lp == null) { LOG.warn("History; Unsafed PK in history: " + entity.getClass().getName() + "." + pd.getName() + "[" + idx + "]" + ob.getClass().getName()); continue; } pkSet.put(lp, ob.getClass()); ++idx; } idx = 0; List<HistProp> hpret = new ArrayList<>(); for (Map.Entry<Long, Class<?>> me : pkSet.entrySet()) { HistProp hp = new HistProp(); hp.setName(me.getValue().toString()); hp.setType(col.getClass().getName() + "<" + me.getValue().getName() + ">"); hp.setValue(me.getValue().toString()); hpret.add(hp); ++idx; } return hpret; }
From source file:com.jhkt.playgroundArena.db.nosql.mongodb.beans.AbstractDocument.java
public final BasicDBObject resolveToBasicDBObject() { BasicDBObject bDBObject = new BasicDBObject(); try {//from w ww . ja va 2s . c om Class<? extends AbstractDocument> currClass = getClass(); while (currClass != null) { for (Method method : currClass.getMethods()) { IDocumentKeyValue dkv = method.getAnnotation(IDocumentKeyValue.class); String mName = method.getName(); if (dkv != null && mName.startsWith(JPAConstants.GETTER_PREFIX)) { try { Object returnValue = method.invoke(this, JPAConstants.EMPTY_OBJECT_ARGUMENT_LIST); char[] propertyChars = mName.substring(3).toCharArray(); String property = String.valueOf(propertyChars[0]).toLowerCase() + String.valueOf(propertyChars, 1, propertyChars.length - 1); if (returnValue == null) { continue; } if (returnValue instanceof AbstractDocument) { Object subReturnValue = RESOLVE_TO_BASIC_DB_OBJECT_METHOD.invoke(returnValue, JPAConstants.EMPTY_OBJECT_ARGUMENT_LIST); bDBObject.put(property, subReturnValue); } else if (returnValue instanceof Enum) { Enum<?> enumClass = Enum.class.cast(returnValue); BasicDBObject enumObject = new BasicDBObject(); enumObject.put(JPAConstants.CONVERTER_CLASS.CLASS.name(), enumClass.getClass().getName()); enumObject.put(JPAConstants.CONVERTER_CLASS.CONTENT.name(), enumClass.name()); bDBObject.put(property, enumObject); } else if (returnValue instanceof Collection) { Collection<?> collectionContent = (Collection<?>) returnValue; BasicDBObject collectionObject = new BasicDBObject(); collectionObject.put(JPAConstants.CONVERTER_CLASS.CLASS.name(), collectionContent.getClass().getName()); BasicDBList bDBList = new BasicDBList(); if (collectionContent.iterator().next() instanceof AbstractDocument) { for (Object content : collectionContent) { if (content instanceof AbstractDocument) { Object subReturnValue = RESOLVE_TO_BASIC_DB_OBJECT_METHOD .invoke(returnValue, JPAConstants.EMPTY_OBJECT_ARGUMENT_LIST); bDBList.add(subReturnValue); } } } else { bDBList.addAll(collectionContent); } collectionObject.put(JPAConstants.CONVERTER_CLASS.CONTENT.name(), bDBList); bDBObject.put(property, collectionObject); } else if (returnValue instanceof Map) { Map<?, ?> mapContent = (Map<?, ?>) returnValue; BasicDBObject mapObject = new BasicDBObject(); mapObject.put(JPAConstants.CONVERTER_CLASS.CLASS.name(), mapContent.getClass().getName()); Set<?> keys = mapContent.keySet(); if (keys.iterator().next() instanceof AbstractDocument) { Map<Object, Object> convertedMap = new HashMap<Object, Object>(); for (Object key : keys) { Object value = mapContent.get(key); Object subReturnValue = RESOLVE_TO_BASIC_DB_OBJECT_METHOD.invoke(value, JPAConstants.EMPTY_OBJECT_ARGUMENT_LIST); convertedMap.put(key, subReturnValue); } mapContent = convertedMap; } mapObject.put(JPAConstants.CONVERTER_CLASS.CONTENT.name(), mapContent); bDBObject.put(property, mapObject); } else { bDBObject.put(property, returnValue); } } catch (Exception e) { } } } currClass = currClass.getSuperclass().asSubclass(AbstractDocument.class); } } catch (ClassCastException castException) { } bDBObject.put(JPAConstants.CONVERTER_CLASS.CLASS.name(), getClass().getName()); _log.info("BdBObject " + bDBObject); return bDBObject; }
From source file:org.malaguna.cmdit.service.reflection.HibernateProxyUtils.java
@SuppressWarnings("unchecked") private Collection<?> deepLoadCollection(Collection collection, Collection guideObj) { Collection result = null;/*from www .j av a2 s . c o m*/ if (guideObj != null && !guideObj.isEmpty() && collection != null && !collection.isEmpty()) { try { if (collection instanceof PersistentSet) { result = new HashSet(); } else if (collection instanceof PersistentList) { result = new ArrayList(); } else { result = collection.getClass().newInstance(); } //Recuperar primera instancia del guideObj y usarlo como siguiente guideObj Object collGuideObj = guideObj.iterator().next(); for (Object aux : collection) { result.add(deepLoad(aux, collGuideObj)); } collection.clear(); collection.addAll(result); } catch (Throwable e) { e.printStackTrace(); } } return collection; }
From source file:org.jdto.mergers.PropertyCollectionMerger.java
/** * Collect a given property of the source collection. * @param values the source collection.// w w w .java2 s . c o m * @param extraParams the name of the property to be merged. * @return a new collection populated with the value of the given property. */ @Override public Collection mergeObjects(Collection values, String[] extraParams) { //this should be null safe if (values == null) { return null; } if (ArrayUtils.isEmpty(extraParams)) { throw new IllegalArgumentException("You need to provide the extra param should be a property name."); } String extraParam = extraParams[0]; Collection retValue = getCollectionToReturn(values); if (retValue == null) { logger.warn("Could not create proper collection of type: " + values.getClass().getName()); return null; } //traverse the collection for (Object value : values) { Object propertyValue = modifier.readPropertyValue(extraParam, value); retValue.add(propertyValue); } return retValue; }