List of usage examples for java.util Set iterator
Iterator<E> iterator();
From source file:de.xwic.appkit.core.model.EntityModelFactory.java
/** * Creates a new entity model based upon the specified entity. * @param entity// w w w .ja v a 2s . c o m * @return * @throws EntityModelException */ @SuppressWarnings("unchecked") public static IEntityModel createModel(IEntity entity) { // create model and InvocationHandler Set<Class<?>> interfacesSet = new HashSet<Class<?>>(); Class<? extends IEntity> clazz = entity.getClass(); interfacesSet.add(IEntityModel.class); // recursivly add all interfaces Class<?> c = clazz; while (c != null) { Class<?>[] orgInterfaces = c.getInterfaces(); for (int i = 0; i < orgInterfaces.length; i++) { interfacesSet.add(orgInterfaces[i]); } c = c.getSuperclass(); } Class[] interfaces = new Class[interfacesSet.size()]; int idx = 0; for (Iterator<Class<?>> it = interfacesSet.iterator(); it.hasNext();) { interfaces[idx++] = it.next(); } EntityModelInvocationHandler ih = new EntityModelInvocationHandler(entity); return (IEntityModel) Proxy.newProxyInstance(classLoader, interfaces, ih); }
From source file:ml.shifu.shifu.core.binning.UpdateBinningInfoReducer.java
private static String limitedFrequentItems(Set<String> fis) { StringBuilder sb = new StringBuilder(200); int size = Math.min(fis.size(), CountAndFrequentItemsWritable.FREQUET_ITEM_MAX_SIZE * 10); Iterator<String> iterator = fis.iterator(); int i = 0;//from ww w .j av a 2 s.co m while (i < size) { String next = iterator.next().replaceAll("\\" + Constants.DEFAULT_DELIMITER, " ").replace(",", " "); sb.append(next); if (i != size - 1) { sb.append(","); } i += 1; } return sb.toString(); }
From source file:com.symbian.driver.plugins.ftptelnet.FtpTransfer.java
/** * removeAllInstances: disconnect all instances and deletes them. * // w ww . ja va 2 s .com * @return boolean success/fail (if any fails to disconnect) */ public static boolean removeAllInstances() { boolean lResult = true; Set<String> lkeySet = iFtpInstances.keySet(); for (Iterator iter = lkeySet.iterator(); iter.hasNext();) { FtpTransfer lInstance = (FtpTransfer) iFtpInstances.get(iter.next()); try { lInstance.disconnectFTP(); iFtpInstances.remove(iter); } catch (IOException lIOException) { lResult = false; LOGGER.log(Level.SEVERE, "Session with transport: " + iter + " failed to disconnect.", lIOException); } } return lResult; }
From source file:I18NUtil.java
/** * Searches for the nearest locale from the available options. To match any locale, pass in * <tt>null</tt>.// ww w . j av a2 s .c o m * * @param templateLocale the template to search for or <tt>null</tt> to match any locale * @param options the available locales to search from * @return Returns the best match from the available options, or the <tt>null</tt> if * all matches fail */ public static Locale getNearestLocale(Locale templateLocale, Set<Locale> options) { if (options.isEmpty()) // No point if there are no options { return null; } else if (templateLocale == null) { for (Locale locale : options) { return locale; } } else if (options.contains(templateLocale)) // First see if there is an exact match { return templateLocale; } // make a copy of the set Set<Locale> remaining = new HashSet<Locale>(options); // eliminate those without matching languages Locale lastMatchingOption = null; String templateLanguage = templateLocale.getLanguage(); if (templateLanguage != null && !templateLanguage.equals("")) { Iterator<Locale> iterator = remaining.iterator(); while (iterator.hasNext()) { Locale option = iterator.next(); if (option != null && !templateLanguage.equals(option.getLanguage())) { iterator.remove(); // It doesn't match, so remove } else { lastMatchingOption = option; // Keep a record of the last match } } } if (remaining.isEmpty()) { return null; } else if (remaining.size() == 1 && lastMatchingOption != null) { return lastMatchingOption; } // eliminate those without matching country codes lastMatchingOption = null; String templateCountry = templateLocale.getCountry(); if (templateCountry != null && !templateCountry.equals("")) { Iterator<Locale> iterator = remaining.iterator(); while (iterator.hasNext()) { Locale option = iterator.next(); if (option != null && !templateCountry.equals(option.getCountry())) { // It doesn't match language - remove // Don't remove the iterator. If it matchs a langage but not the country, returns any matched language // iterator.remove(); } else { lastMatchingOption = option; // Keep a record of the last match } } } /*if (remaining.isEmpty()) { return null; } else */ if (remaining.size() == 1 && lastMatchingOption != null) { return lastMatchingOption; } else { // We have done an earlier equality check, so there isn't a matching variant // Also, we know that there are multiple options at this point, either of which will do. // This gets any country match (there will be worse matches so we take the last the country match) if (lastMatchingOption != null) { return lastMatchingOption; } else { for (Locale locale : remaining) { return locale; } } } // The logic guarantees that this code can't be called throw new RuntimeException("Logic should not allow code to get here."); }
From source file:ClassFileUtilities.java
private static void computeClassDependencies(InputStream is, Set classpath, Set done, Set result, boolean rec) throws IOException { Iterator it = getClassDependencies(is).iterator(); while (it.hasNext()) { String s = (String) it.next(); if (!done.contains(s)) { done.add(s);// w ww . j a v a 2 s . c om Iterator cpit = classpath.iterator(); while (cpit.hasNext()) { InputStream depis = null; String path = null; Object cpEntry = cpit.next(); if (cpEntry instanceof JarFile) { JarFile jarFile = (JarFile) cpEntry; String classFileName = s + ".class"; ZipEntry ze = jarFile.getEntry(classFileName); if (ze != null) { path = jarFile.getName() + '!' + classFileName; depis = jarFile.getInputStream(ze); } } else { path = ((String) cpEntry) + '/' + s + ".class"; File f = new File(path); if (f.isFile()) { depis = new FileInputStream(f); } } if (depis != null) { result.add(path); if (rec) { computeClassDependencies(depis, classpath, done, result, rec); } } } } } }
From source file:com.gigaspaces.internal.utils.ClassLoaderCleaner.java
/** * Clear the {@link ResourceBundle} cache of any bundles loaded by this class loader or any * class loader where this loader is a parent class loader. * * The ResourceBundle is using WeakReferences so it shouldn't be pinning the class loader in * memory. However, it is. Therefore clear ou the references. *///from ww w . j av a2 s . c o m private static void clearReferencesResourceBundles(ClassLoader classLoader) { // Get a reference to the cache try { Field cacheListField = ResourceBundle.class.getDeclaredField("cacheList"); cacheListField.setAccessible(true); // Java 6 uses ConcurrentMap // Java 5 uses SoftCache extends Abstract Map // So use Map and it *should* work with both Map<?, ?> cacheList = (Map<?, ?>) cacheListField.get(null); // Get the keys (loader references are in the key) Set<?> keys = cacheList.keySet(); Field loaderRefField = null; // Iterate over the keys looking at the loader instances Iterator<?> keysIter = keys.iterator(); int countRemoved = 0; while (keysIter.hasNext()) { Object key = keysIter.next(); if (loaderRefField == null) { loaderRefField = key.getClass().getDeclaredField("loaderRef"); loaderRefField.setAccessible(true); } WeakReference<?> loaderRef = (WeakReference<?>) loaderRefField.get(key); ClassLoader loader = (ClassLoader) loaderRef.get(); while (loader != null && loader != classLoader) { loader = loader.getParent(); } if (loader != null) { keysIter.remove(); countRemoved++; } } if (countRemoved > 0 && logger.isLoggable(Level.FINE)) { logger.fine("Removed [" + countRemoved + "] ResourceBundle references from the cache"); } } catch (Exception e) { logger.log(Level.WARNING, "Failed to clear ResourceBundle references", e); } }
From source file:ListAlgorithms.java
public static void printSet(String setName, Set algorithms) { System.out.println(setName + ":"); if (algorithms.isEmpty()) { System.out.println(" None available."); } else {// w w w . j a va 2s . c om Iterator it = algorithms.iterator(); while (it.hasNext()) { String name = (String) it.next(); System.out.println(" " + name); } } }
From source file:com.zving.cms.site.Site.java
public static void addDefaultPriv(long siteID, Transaction trans) { Set set = Priv.SITE_MAP.keySet(); String code;/*from www.j ava 2s. co m*/ ZDPrivilegeSchema priv; for (Iterator iter = set.iterator(); iter.hasNext();) { code = (String) iter.next(); priv = new ZDPrivilegeSchema(); priv.setOwnerType("R"); priv.setOwner("admin"); priv.setID(String.valueOf(siteID)); priv.setPrivType("site"); priv.setCode(code); priv.setValue("1"); trans.add(priv, 1); } set = Priv.ARTICLE_MAP.keySet(); for (Iterator iter = set.iterator(); iter.hasNext();) { code = (String) iter.next(); priv = new ZDPrivilegeSchema(); priv.setOwnerType("R"); priv.setOwner("admin"); priv.setID(String.valueOf(siteID)); priv.setPrivType("site"); priv.setCode(code); priv.setValue("1"); trans.add(priv, 1); } }
From source file:net.sourceforge.fenixedu.domain.space.SpaceUtils.java
public static Occupation getFirstOccurrenceOfResourceAllocationByClass(final Space space, final Lesson lesson) { for (final Occupation resourceAllocation : space.getOccupationSet()) { if (resourceAllocation instanceof LessonInstanceSpaceOccupation) { final LessonInstanceSpaceOccupation lessonInstanceSpaceOccupation = (LessonInstanceSpaceOccupation) resourceAllocation; final Set<LessonInstance> instancesSet = lessonInstanceSpaceOccupation.getLessonInstancesSet(); if (!instancesSet.isEmpty() && instancesSet.iterator().next().getLesson() == lesson) { return resourceAllocation; }//from w w w . j a va2s.c om } } return null; }
From source file:org.jboss.as.test.integration.logging.formatters.XmlFormatterTestCase.java
private static void validateDefault(final Document doc, final Collection<String> expectedKeys, final String expectedMessage) { final Set<String> remainingKeys = new HashSet<>(expectedKeys); String loggerClassName = null; String loggerName = null;// w w w .ja va 2s . c o m String level = null; String message = null; // Check all the expected keys final Iterator<String> iterator = remainingKeys.iterator(); while (iterator.hasNext()) { final String key = iterator.next(); final NodeList children = doc.getElementsByTagName(key); Assert.assertTrue(String.format("Key %s was not found in the document: %s", key, doc), children.getLength() > 0); final Node child = children.item(0); if ("loggerClassName".equals(child.getNodeName())) { loggerClassName = child.getTextContent(); } else if ("loggerName".equals(child.getNodeName())) { loggerName = child.getTextContent(); } else if ("level".equals(child.getNodeName())) { level = child.getTextContent(); } else if ("message".equals(child.getNodeName())) { message = child.getTextContent(); } iterator.remove(); } // Should have no more remaining keys Assert.assertTrue("There are remaining keys that were not validated: " + remainingKeys, remainingKeys.isEmpty()); Assert.assertEquals("org.jboss.logging.Logger", loggerClassName); Assert.assertEquals(LoggingServiceActivator.LOGGER.getName(), loggerName); Assert.assertTrue("Invalid level found in " + level, isValidLevel(level)); Assert.assertEquals(expectedMessage, message); }