List of usage examples for java.util TreeSet size
public int size()
From source file:org.auscope.gridtools.RegistryQueryClient.java
/** * Gets all GridFTP servers available on the Grid. * /* w w w .ja v a2 s .c om*/ * @return Array of hostnames of available GridFTP servers. */ public String[] getAllGridFTPServersOnGrid() { String[] serverNames = new String[0]; String xpathQuery = "//*[local-name()='Site']" + "/child::node()[local-name()='StorageElement']" + "/child::node()[local-name()='AccessProtocol']" + "/child::node()[local-name()='Type']" + "[text()='gsiftp']/parent::node()"; // Query MDS file. NodeList ftpServersList = turboMDSquery(xpathQuery); if (ftpServersList != null) { // Keep unique using TreeSet TreeSet<String> myTreeSet = new TreeSet<String>(); for (int i = 0; i < ftpServersList.getLength(); i++) { Element siteEl = (Element) ftpServersList.item(i); myTreeSet.add(getTextValue(siteEl, "Endpoint")); } // Shove in array serverNames = myTreeSet.toArray(new String[myTreeSet.size()]); } return serverNames; }
From source file:org.auscope.gridtools.RegistryQueryClient.java
/** * Gets the storage path that satisfies the amount of disk space available * at a storage element.//from w ww. ja va 2 s.c o m * * <b>TODO: Not implemented properly yet...</b> * * @param defaultSE The storage element to check * @param diskSpace The amount of diskspace required * @return The storage path that satisfies these requirements */ public String getStoragePathWithSpaceAvailFromDefaultStorageElement(String defaultSE, String diskSpace) { /* * //*[local-name()='Site']/child::node()[local-name()='StorageElement'] * [@UniqueID='defaultSE']/child::node()[local-name()='StorageArea'] * /child::node()[local-name()='AvailableSpace'][number(text())>diskSpace]/parent::node() * /child::node()[local-name()='Path'] */ String storagePath = ""; String xpathQuery = "//*[local-name()='Site']" + "/child::node()[local-name()='Cluster']" + "/child::node()[local-name()='ComputingElement']" + "/child::node()[local-name()='Name'][text()='queueName']" + "/parent::node()"; // Parse the document NodeList pathNodeList = turboMDSquery(xpathQuery); if (pathNodeList != null) { TreeSet<String> myTreeSet = new TreeSet<String>(); for (int i = 0; i < pathNodeList.getLength(); i++) { Element siteEl = (Element) pathNodeList.item(i); myTreeSet.add(getTextValue(siteEl, "DefaultSE")); } // TODO: THIS IS WRONG. storagePath = myTreeSet.toArray(new String[myTreeSet.size()])[0]; } return storagePath; }
From source file:org.auscope.gridtools.RegistryQueryClient.java
/** * Gets the email address of a site's support contact * /* ww w . ja va 2 s . c om*/ * @param site The site to check * @return The singular site email address string */ public String getSiteContactEmailAtSite(String site) { String xpathQuery = "//*[local-name()='Site']/child::node()[local-name()='Name']" + "[translate(text(),'abcdefghijklmnopqrstuvwxyz'," + "'ABCDEFGHIJKLMNOPQRSTUVWXYZ')=translate('" + site + "'," + "'abcdefghijklmnopqrstuvwxyz','ABCDEFGHIJKLMNOPQRSTUVWXYZ')]" + "/parent::node()"; String email = ""; // Parse the document NodeList emailNodeList = turboMDSquery(xpathQuery); if (emailNodeList != null) { TreeSet<String> myTreeSet = new TreeSet<String>(); for (int i = 0; i < emailNodeList.getLength(); i++) { Element siteEl = (Element) emailNodeList.item(i); myTreeSet.add(getTextValue(siteEl, "UserSupportContact")); } // Take the first element.... pretty poor way to do it... email = myTreeSet.toArray(new String[myTreeSet.size()])[0]; } return email; }
From source file:spectrogram.Spectrogram.java
/** * Using previously set up object members verify the channel and get needed info, complication is * when the channel is only partially specified * * @return true if we got what we needed, else return after we've printed errors. *//* ww w .j a va2 s . c o m*/ private boolean getChanInfo() throws SQLException { boolean ret = false; long strt = System.currentTimeMillis(); { int n; if (channelName == null || channelName.isEmpty()) { throw new IllegalArgumentException("No Channel specified"); } if ((server == null || server.isEmpty()) && (cType == null || cType.isEmpty())) { n = chanTbl.getBestMatch(channelName); chanInfo = chanTbl.getChanInfo(n); server = chanInfo.getServer(); } else { if (cType == null || cType.isEmpty()) { cType = "raw"; } TreeSet<ChanInfo> chSet = chanTbl.getAsSet(server, channelName, cType, 10); if (chSet.size() > 1) { chanInfo = null; if (sampleRate > 0) { for (ChanInfo ci : chSet) { if (Math.abs(ci.getRate() - sampleRate) < .0001) { chanInfo = ci; break; } } } if (chanInfo == null) { System.err.print("Warning: more than one channel matches: " + channelName); System.err.println(" and no applicable rate specified."); } } else if (chSet.size() == 1) { chanInfo = chSet.first(); } } if (chanInfo == null) { System.err.println("Channel requested was not found: " + channelName); } else { sampleRate = chanInfo.getRate(); String dtyp = chanInfo.getdType(); if (dtyp.equalsIgnoreCase("INT-16")) { bytesPerSample = 2; } else if (dtyp.equalsIgnoreCase("INT-32")) { bytesPerSample = 4; } else if (dtyp.equalsIgnoreCase("INT-64")) { bytesPerSample = 8; } else if (dtyp.equalsIgnoreCase("FLT-32")) { bytesPerSample = 4; } else if (dtyp.equalsIgnoreCase("FLT-64")) { bytesPerSample = 8; } else if (dtyp.equalsIgnoreCase("CPX-64")) { bytesPerSample = 8; } ret = true; float dur = (System.currentTimeMillis() - strt) / 1000.f; System.out.format("Get channel info took %1$.1f sec.\n", dur); } } return ret; }
From source file:com.offbynull.voip.kademlia.FindSubcoroutine.java
@Override public List<Node> run(Continuation cnt) throws Exception { Context ctx = (Context) cnt.getContext(); ctx.addOutgoingMessage(subAddress, logAddress, info("Finding {}", findId)); // Set up subcoroutine router Address routerAddress = subAddress.appendSuffix("finderreq" + idGenerator.generate()); SubcoroutineRouter msgRouter = new SubcoroutineRouter(routerAddress, ctx); Controller msgRouterController = msgRouter.getController(); // Get initial set of nodes to query from routing table List<Node> startNodes = router.find(findId, maxResults, false); // do not include stale nodes, we only want to contact alive nodes ctx.addOutgoingMessage(subAddress, logAddress, info("Route table entries closest to {}: {}", findId, startNodes)); // Create sorted set of nodes to contact IdXorMetricComparator idClosenessComparator = new IdXorMetricComparator(findId); TreeSet<Node> contactSet = new TreeSet<>((x, y) -> idClosenessComparator.compare(x.getId(), y.getId())); contactSet.addAll(startNodes);/*from ww w . jav a 2 s. c o m*/ // Create a sorted set of nodes to retain closest nodes in TreeSet<Node> closestSet = new TreeSet<>((x, y) -> idClosenessComparator.compare(x.getId(), y.getId())); // Execute requests Map<Subcoroutine<?>, Node> requestSubcoroutineToNodes = new HashMap<>(); // executing requests Set<Id> queriedSet = new HashSet<>(); // ids that have already been queried while (true) { // If there's room left to query more contacts that are closer to findId, do so... while (msgRouterController.size() < maxConcurrentRequests && !contactSet.isEmpty()) { // Get next farthest away node to contact Node contactNode = contactSet.pollLast(); // Add it to set of set of ids that have already been queried.. if it's already there, it means that it's already been // queried by this find, so skip it... boolean added = queriedSet.add(contactNode.getId()); if (!added) { continue; } // Add it to the set of closest nodes (will be removed if node fails to respond) closestSet.add(contactNode); // If we already have maxResult closer nodes to findId, skip this node if (closestSet.size() > maxResults) { Node removedNode = closestSet.pollLast(); if (removedNode == contactNode) { continue; } } // Initialize query Address destinationAddress = addressTransformer.toAddress(contactNode.getLink()) .appendSuffix(ROUTER_EXT_HANDLER_RELATIVE_ADDRESS); RequestSubcoroutine<FindResponse> reqSubcoroutine = new RequestSubcoroutine.Builder<FindResponse>() .sourceAddress(routerAddress, idGenerator).destinationAddress(destinationAddress) .timerAddress(timerAddress) .request(new FindRequest(advertiseSelf ? baseId : null, findId, maxResults)) .addExpectedResponseType(FindResponse.class).attemptInterval(Duration.ofSeconds(2L)) .maxAttempts(5).throwExceptionIfNoResponse(false).build(); ctx.addOutgoingMessage(subAddress, logAddress, info("Querying node {}", contactNode)); // Add query to router msgRouterController.add(reqSubcoroutine, AddBehaviour.ADD_PRIME_NO_FINISH); requestSubcoroutineToNodes.put(reqSubcoroutine, contactNode); } // If there are no more requests running, it means we're finished if (msgRouterController.size() == 0) { ctx.addOutgoingMessage(subAddress, logAddress, info("Find complete: {}", closestSet)); return new ArrayList<>(closestSet); } // Wait for next messange forward to the router cnt.suspend(); ForwardResult fr = msgRouter.forward(); // If a request completed from the forwarded message if (fr.isForwarded() && fr.isCompleted()) { // calling isCompleted by itself may throw an exception, check isForwarded first // Get response FindResponse findResponse = (FindResponse) fr.getResult(); if (findResponse == null) { // If failure, then mark as stale and remove from closest // DONT BOTHER WITH TRYING TO CALCULATE LOCKING/UNLOCKING LOGIC. THE LOGIC WILL BECOME EXTREMELY CONVOLUTED. THE QUERY // DID 5 REQUEST. IF NO ANSWER WAS GIVEN IN THE ALLOTED TIME, THEN MARK AS STALE! Node contactedNode = requestSubcoroutineToNodes.remove(fr.getSubcoroutine()); try { // not allowed to mark self as stale -- we may want to find self, but if we do and it's not responsive dont try to // mark it as stale if (!contactedNode.getId().equals(baseId)) { router.stale(contactedNode); } } catch (NodeNotFoundException nnfe) { // may have been removed (already marked as stale) / may not be in routing tree // Do nothing } closestSet.remove(contactedNode); } else { // If success, then add returned nodes to contacts Node[] nodes = findResponse.getNodes(); contactSet.addAll(Arrays.asList(nodes)); // If we don't want to find our own ID / query ourselves... remove any reference to our own ID in the contactSet // TODO: optimize this by removing before it's added to contactSet if (ignoreSelf) { contactSet.removeIf(x -> x.getId().equals(baseId)); } } } } }
From source file:org.unitime.timetable.onlinesectioning.OnlineSectioningServerImpl.java
@Override public void remove(Offering offering) { iLock.writeLock().lock();//from w w w . j a v a2 s . c o m try { for (Course course : offering.getCourses()) { CourseInfo ci = iCourseForId.get(course.getId()); if (ci != null) { TreeSet<CourseInfo> courses = iCourseForName.get(ci.toString()); if (courses != null) { courses.remove(ci); if (courses.isEmpty()) { iCourseForName.remove(ci.toString()); } else if (courses.size() == 1) { for (CourseInfo x : courses) x.setHasUniqueName(true); } } iCourseForId.remove(ci.getUniqueId()); iCourses.remove(ci); } iCourseTable.remove(course.getId()); } iOfferingTable.remove(offering.getId()); for (Config config : offering.getConfigs()) { for (Subpart subpart : config.getSubparts()) for (Section section : subpart.getSections()) iClassTable.remove(section.getId()); for (Enrollment enrollment : new ArrayList<Enrollment>(config.getEnrollments())) enrollment.variable().unassign(0); } } finally { iLock.writeLock().unlock(); } }
From source file:org.apache.cassandra.concurrent.LongSharedExecutorPoolTest.java
private void testPromptnessOfExecution(long intervalNanos, float loadIncrement) throws InterruptedException, ExecutionException { final int executorCount = 4; int threadCount = 8; int maxQueued = 1024; final WeibullDistribution workTime = new WeibullDistribution(3, 200000); final long minWorkTime = TimeUnit.MICROSECONDS.toNanos(1); final long maxWorkTime = TimeUnit.MILLISECONDS.toNanos(1); final int[] threadCounts = new int[executorCount]; final WeibullDistribution[] workCount = new WeibullDistribution[executorCount]; final ExecutorService[] executors = new ExecutorService[executorCount]; for (int i = 0; i < executors.length; i++) { executors[i] = SharedExecutorPool.SHARED.newExecutor(threadCount, maxQueued, "test" + i, "test" + i); threadCounts[i] = threadCount;//ww w . j av a 2 s . c o m workCount[i] = new WeibullDistribution(2, maxQueued); threadCount *= 2; maxQueued *= 2; } long runs = 0; long events = 0; final TreeSet<Batch> pending = new TreeSet<>(); final BitSet executorsWithWork = new BitSet(executorCount); long until = 0; // basic idea is to go through different levels of load on the executor service; initially is all small batches // (mostly within max queue size) of very short operations, moving to progressively larger batches // (beyond max queued size), and longer operations for (float multiplier = 0f; multiplier < 2.01f;) { if (System.nanoTime() > until) { System.out.println(String.format("Completed %.0fK batches with %.1fM events", runs * 0.001f, events * 0.000001f)); events = 0; until = System.nanoTime() + intervalNanos; multiplier += loadIncrement; System.out.println(String.format("Running for %ds with load multiplier %.1f", TimeUnit.NANOSECONDS.toSeconds(intervalNanos), multiplier)); } // wait a random amount of time so we submit new tasks in various stages of long timeout; if (pending.isEmpty()) timeout = 0; else if (Math.random() > 0.98) timeout = Long.MAX_VALUE; else if (pending.size() == executorCount) timeout = pending.first().timeout; else timeout = (long) (Math.random() * pending.last().timeout); while (!pending.isEmpty() && timeout > System.nanoTime()) { Batch first = pending.first(); boolean complete = false; try { for (Result result : first.results.descendingSet()) result.future.get(timeout - System.nanoTime(), TimeUnit.NANOSECONDS); complete = true; } catch (TimeoutException e) { } if (!complete && System.nanoTime() > first.timeout) { for (Result result : first.results) if (!result.future.isDone()) throw new AssertionError(); complete = true; } if (complete) { pending.pollFirst(); executorsWithWork.clear(first.executorIndex); } } // if we've emptied the executors, give all our threads an opportunity to spin down if (timeout == Long.MAX_VALUE) Uninterruptibles.sleepUninterruptibly(10, TimeUnit.MILLISECONDS); // submit a random batch to the first free executor service int executorIndex = executorsWithWork.nextClearBit(0); if (executorIndex >= executorCount) continue; executorsWithWork.set(executorIndex); ExecutorService executor = executors[executorIndex]; TreeSet<Result> results = new TreeSet<>(); int count = (int) (workCount[executorIndex].sample() * multiplier); long targetTotalElapsed = 0; long start = System.nanoTime(); long baseTime; if (Math.random() > 0.5) baseTime = 2 * (long) (workTime.sample() * multiplier); else baseTime = 0; for (int j = 0; j < count; j++) { long time; if (baseTime == 0) time = (long) (workTime.sample() * multiplier); else time = (long) (baseTime * Math.random()); if (time < minWorkTime) time = minWorkTime; if (time > maxWorkTime) time = maxWorkTime; targetTotalElapsed += time; Future<?> future = executor.submit(new WaitTask(time)); results.add(new Result(future, System.nanoTime() + time)); } long end = start + (long) Math.ceil(targetTotalElapsed / (double) threadCounts[executorIndex]) + TimeUnit.MILLISECONDS.toNanos(100L); long now = System.nanoTime(); if (runs++ > executorCount && now > end) throw new AssertionError(); events += results.size(); pending.add(new Batch(results, end, executorIndex)); // System.out.println(String.format("Submitted batch to executor %d with %d items and %d permitted millis", executorIndex, count, TimeUnit.NANOSECONDS.toMillis(end - start))); } }
From source file:bes.injector.InjectorBurnTest.java
private void testPromptnessOfExecution(long intervalNanos, float loadIncrement) throws InterruptedException, ExecutionException, TimeoutException { final int executorCount = 4; int threadCount = 8; int maxQueued = 1024; final WeibullDistribution workTime = new WeibullDistribution(3, 200000); final long minWorkTime = TimeUnit.MICROSECONDS.toNanos(1); final long maxWorkTime = TimeUnit.MILLISECONDS.toNanos(1); final int[] threadCounts = new int[executorCount]; final WeibullDistribution[] workCount = new WeibullDistribution[executorCount]; final ExecutorService[] executors = new ExecutorService[executorCount]; final Injector injector = new Injector(""); for (int i = 0; i < executors.length; i++) { executors[i] = injector.newExecutor(threadCount, maxQueued); threadCounts[i] = threadCount;/*from w w w . j av a2 s . c om*/ workCount[i] = new WeibullDistribution(2, maxQueued); threadCount *= 2; maxQueued *= 2; } long runs = 0; long events = 0; final TreeSet<Batch> pending = new TreeSet<Batch>(); final BitSet executorsWithWork = new BitSet(executorCount); long until = 0; // basic idea is to go through different levels of load on the executor service; initially is all small batches // (mostly within max queue size) of very short operations, moving to progressively larger batches // (beyond max queued size), and longer operations for (float multiplier = 0f; multiplier < 2.01f;) { if (System.nanoTime() > until) { System.out.println(String.format("Completed %.0fK batches with %.1fM events", runs * 0.001f, events * 0.000001f)); events = 0; until = System.nanoTime() + intervalNanos; multiplier += loadIncrement; System.out.println(String.format("Running for %ds with load multiplier %.1f", TimeUnit.NANOSECONDS.toSeconds(intervalNanos), multiplier)); } // wait a random amount of time so we submit new tasks in various stages of long timeout; if (pending.isEmpty()) timeout = 0; else if (Math.random() > 0.98) timeout = Long.MAX_VALUE; else if (pending.size() == executorCount) timeout = pending.first().timeout; else timeout = (long) (Math.random() * pending.last().timeout); while (!pending.isEmpty() && timeout > System.nanoTime()) { Batch first = pending.first(); boolean complete = false; try { for (Result result : first.results.descendingSet()) result.future.get(timeout - System.nanoTime(), TimeUnit.NANOSECONDS); complete = true; } catch (TimeoutException e) { } if (!complete && System.nanoTime() > first.timeout) { for (Result result : first.results) if (!result.future.isDone()) throw new AssertionError(); complete = true; } if (complete) { pending.pollFirst(); executorsWithWork.clear(first.executorIndex); } } // if we've emptied the executors, give all our threads an opportunity to spin down if (timeout == Long.MAX_VALUE) { try { Thread.sleep(10); } catch (InterruptedException e) { } } // submit a random batch to the first free executor service int executorIndex = executorsWithWork.nextClearBit(0); if (executorIndex >= executorCount) continue; executorsWithWork.set(executorIndex); ExecutorService executor = executors[executorIndex]; TreeSet<Result> results = new TreeSet<Result>(); int count = (int) (workCount[executorIndex].sample() * multiplier); long targetTotalElapsed = 0; long start = System.nanoTime(); long baseTime; if (Math.random() > 0.5) baseTime = 2 * (long) (workTime.sample() * multiplier); else baseTime = 0; for (int j = 0; j < count; j++) { long time; if (baseTime == 0) time = (long) (workTime.sample() * multiplier); else time = (long) (baseTime * Math.random()); if (time < minWorkTime) time = minWorkTime; if (time > maxWorkTime) time = maxWorkTime; targetTotalElapsed += time; Future<?> future = executor.submit(new WaitTask(time)); results.add(new Result(future, System.nanoTime() + time)); } long end = start + (long) Math.ceil(targetTotalElapsed / (double) threadCounts[executorIndex]) + TimeUnit.MILLISECONDS.toNanos(100L); long now = System.nanoTime(); if (runs++ > executorCount && now > end) throw new AssertionError(); events += results.size(); pending.add(new Batch(results, end, executorIndex)); // System.out.println(String.format("Submitted batch to executor %d with %d items and %d permitted millis", executorIndex, count, TimeUnit.NANOSECONDS.toMillis(end - start))); } }
From source file:org.openpnp.machine.reference.camera.TableScannerCamera.java
private synchronized void initialize() throws Exception { stop();//from ww w . j a va 2 s .com sourceUrl = new URL(sourceUri); cacheDirectory = new File(Configuration.get().getResourceDirectory(getClass()), DigestUtils.shaHex(sourceUri)); if (!cacheDirectory.exists()) { cacheDirectory.mkdirs(); } File[] files = null; // Attempt to get the list of files from the source. try { files = loadSourceFiles(); } catch (Exception e) { logger.warn("Unable to load file list from {}", sourceUri); logger.warn("Reason", e); } if (files == null) { files = loadCachedFiles(); } if (files.length == 0) { throw new Exception("No source or cached files found."); } // Load the first image we found and use it's properties as a template // for the rest of the images. BufferedImage templateImage = new Tile(0, 0, files[0]).getImage(); width = templateImage.getWidth(); height = templateImage.getHeight(); tileList = new ArrayList<Tile>(); lastX = Double.MIN_VALUE; lastY = Double.MIN_VALUE; lastCenterTile = null; // We build a set of unique X and Y positions that we see so we can // later build a two dimensional array of the riles TreeSet<Double> uniqueX = new TreeSet<Double>(); TreeSet<Double> uniqueY = new TreeSet<Double>(); // Create a map of the tiles so that we can quickly find them when we // build the array. Map<Tile, Tile> tileMap = new HashMap<Tile, Tile>(); // Parse the filenames of the all the files and add their coordinates // to the sets and map. for (File file : files) { String filename = file.getName(); filename = filename.substring(0, filename.indexOf(".png")); String[] xy = filename.split(","); double x = Double.parseDouble(xy[0]); double y = Double.parseDouble(xy[1]); Tile tile = new Tile(x, y, file); uniqueX.add(x); uniqueY.add(y); tileMap.put(tile, tile); tileList.add(tile); } // Create a two dimensional array to store all the of the tiles tiles = new Tile[uniqueX.size()][uniqueY.size()]; // Iterate through all the unique X and Y positions that were found // and add each file to the two dimensional array in the position // where it belongs int x = 0, y = 0; for (Double xPos : uniqueX) { y = 0; for (Double yPos : uniqueY) { Tile tile = tileMap.get(new Tile(xPos, yPos, null)); tiles[x][y] = tile; tile.setTileX(x); tile.setTileY(y); y++; } x++; } /* * Create a buffer that we will render the center tile and it's * surrounding tiles to. */ buffer = new BufferedImage(templateImage.getWidth() * tilesWide, templateImage.getHeight() * tilesHigh, BufferedImage.TYPE_INT_ARGB); if (listeners.size() > 0) { start(); } }
From source file:org.auscope.gridtools.RegistryQueryClient.java
/** * Retrieves all codes (software packages) at a particular site. * /* ww w .ja v a2 s.com*/ * @param site The name of the site * @return An array of codes available at the site */ public String[] getAllCodesAtSite(String site) { String[] siteCodesAvail = new String[0]; // XPath query to get codes (SoftwarePackages) available at a given site String xpathQuery = "//*[local-name()='Site']/child::node()" + "[contains(name(),Name)][text()='" + site + "']" + "/ancestor::node()[local-name()='Site']" + "/descendant::node()[local-name()='SoftwarePackage']"; // Parse the document NodeList siteSWPackageNodeList = turboMDSquery(xpathQuery); if (siteSWPackageNodeList != null) { TreeSet<String> myTreeSet = new TreeSet<String>(); // Iterate through the document to get SoftwarePackage's name. for (int i = 0; i < siteSWPackageNodeList.getLength(); i++) { // Get SoftwarePackage name. Element siteEl = (Element) siteSWPackageNodeList.item(i); myTreeSet.add(getTextValue(siteEl, "Name")); } siteCodesAvail = myTreeSet.toArray(new String[myTreeSet.size()]); } return siteCodesAvail; }