List of usage examples for java.util SortedSet first
E first();
From source file:org.apache.geronimo.console.databasemanager.wizard.DatabasePoolPortlet.java
private static File getRAR(PortletRequest request, String rarPath) { org.apache.geronimo.kernel.repository.Artifact artifact = org.apache.geronimo.kernel.repository.Artifact .create(rarPath);//w w w .j a v a 2 s. co m ListableRepository[] repos = PortletManager.getCurrentServer(request).getRepositories(); for (ListableRepository repo : repos) { // if the artifact is not fully resolved then try to resolve it if (!artifact.isResolved()) { SortedSet results = repo.list(artifact); if (!results.isEmpty()) { artifact = (Artifact) results.first(); } else { continue; } } File url = repo.getLocation(artifact); if (url != null) { if (url.exists() && url.canRead() && !url.isDirectory()) { return url; } } } return null; }
From source file:gov.nih.nci.cabig.caaers.domain.SiteResearchStaff.java
/** * Gets the active date.//from w ww. j a v a 2s . com * * @return the active date */ @Transient public Date getActiveDate() { SortedSet<Date> dates = new TreeSet<Date>(); if (this.getSiteResearchStaffRoles() == null) return new Date(System.currentTimeMillis()); for (SiteResearchStaffRole srsr : this.getSiteResearchStaffRoles()) { if (srsr.getStartDate() == null) srsr.setStartDate(new Date(System.currentTimeMillis())); dates.add(srsr.getStartDate()); } if (dates.size() > 0) return dates.first(); else return null; }
From source file:org.wrml.runtime.DefaultModel.java
private final Keys buildKeys(final URI schemaUri, final Map<String, Object> readOnlySlotMap, final KeysBuilder keysBuilder) { final Context context = getContext(); final SchemaLoader schemaLoader = context.getSchemaLoader(); final Prototype prototype = schemaLoader.getPrototype(schemaUri); final SortedSet<String> keySlotNames = prototype.getDeclaredKeySlotNames(); if (keySlotNames != null && !keySlotNames.isEmpty()) { final Object keyValue; if (keySlotNames.size() == 1) { final String keySlotName = keySlotNames.first(); if (readOnlySlotMap.containsKey(keySlotName)) { keyValue = readOnlySlotMap.get(keySlotName); } else { keyValue = null;//w w w .j ava2 s .c om } } else { final SortedMap<String, Object> keySlots = new TreeMap<String, Object>(); for (final String keySlotName : keySlotNames) { final Object keySlotValue = readOnlySlotMap.get(keySlotName); keySlots.put(keySlotName, keySlotValue); } keyValue = new CompositeKey(keySlots); } if (keyValue != null) { keysBuilder.addKey(schemaUri, keyValue); } } final Set<URI> baseSchemaUris = prototype.getAllBaseSchemaUris(); if (baseSchemaUris != null && !baseSchemaUris.isEmpty()) { for (final URI baseSchemaUri : baseSchemaUris) { buildKeys(baseSchemaUri, readOnlySlotMap, keysBuilder); } } return keysBuilder.toKeys(); }
From source file:org.wrml.runtime.rest.ApiNavigator.java
public UUID getResourceTemplateId(final URI uri) { final SortedSet<ResourceMatchResult> results = match(uri); if (results == null || results.isEmpty()) { return null; }/*from w ww .j a va 2 s. c o m*/ final ResourceMatchResult result = results.first(); final Resource resource = result.getResource(); return resource.getResourceTemplateId(); }
From source file:com.alibaba.otter.shared.arbitrate.impl.zookeeper.lock.DistributedLock.java
/** * lock??watch????lock?//from ww w. ja v a 2s .c om */ private Boolean acquireLock(final BooleanMutex mutex) { try { do { if (id == null) {// ?lock long sessionId = getSessionId(); String prefix = "x-" + sessionId + "-"; // String path = zookeeper.create(root + "/" + prefix, data, CreateMode.EPHEMERAL_SEQUENTIAL); int index = path.lastIndexOf("/"); id = StringUtils.substring(path, index + 1); idName = new LockNode(id); } if (id != null) { List<String> names = zookeeper.getChildren(root); if (names.isEmpty()) { logger.warn("lock lost with scene:empty list, id[] and node[]", id, idName); unlock();// ?? } else { // ? SortedSet<LockNode> sortedNames = new TreeSet<LockNode>(); for (String name : names) { sortedNames.add(new LockNode(name)); } if (sortedNames.contains(idName) == false) { logger.warn("lock lost with scene:not contains ,id[] and node[]", id, idName); unlock();// ?? continue; } // ?ownerId ownerId = sortedNames.first().getName(); if (mutex != null && isOwner()) { mutex.set(true);// ? return true; } else if (mutex == null) { return isOwner(); } SortedSet<LockNode> lessThanMe = sortedNames.headSet(idName); if (!lessThanMe.isEmpty()) { // ? LockNode lastChildName = lessThanMe.last(); lastChildId = lastChildName.getName(); // watcher? IZkConnection connection = zookeeper.getConnection(); // zkclient?zk?lock??watcher?zk? ZooKeeper orginZk = ((ZooKeeperx) connection).getZookeeper(); Stat stat = orginZk.exists(root + "/" + lastChildId, new AsyncWatcher() { public void asyncProcess(WatchedEvent event) { if (!mutex.state()) { // ?????lock acquireLock(mutex); } else { logger.warn("locked successful."); } } }); if (stat == null) { acquireLock(mutex);// ????watcher? } } else { if (isOwner()) { mutex.set(true); } else { logger.warn("lock lost with scene:no less ,id[] and node[]", id, idName); unlock();// ?idownerId?? } } } } } while (id == null); } catch (KeeperException e) { exception = e; if (mutex != null) { mutex.set(true); } } catch (InterruptedException e) { interrupt = e; if (mutex != null) { mutex.set(true); } } catch (Throwable e) { other = e; if (mutex != null) { mutex.set(true); } } if (isOwner() && mutex != null) { mutex.set(true); } return Boolean.FALSE; }
From source file:org.jasig.schedassist.model.AvailableBlockBuilderTest.java
/** * Create adjacent available blocks but set different meeting locations. * Confirm blocks not combined by combine method. * @throws InputFormatException //from w w w . jav a 2s . c o m * */ @Test public void testDifferentMeetingLocationNotCombined() throws InputFormatException { AvailableBlock blockNoLocation = AvailableBlockBuilder.createBlock("20110810-0900", "20110810-0930", 1, "some location"); AvailableBlock blockWithLocation = AvailableBlockBuilder.createBlock("20110810-0930", "20110810-1000", 1, "different location"); SortedSet<AvailableBlock> smallBlocks = new TreeSet<AvailableBlock>(); smallBlocks.add(blockNoLocation); smallBlocks.add(blockWithLocation); SortedSet<AvailableBlock> combined = AvailableBlockBuilder.combine(smallBlocks); Assert.assertEquals(2, combined.size()); Assert.assertEquals(blockNoLocation, combined.first()); Assert.assertEquals(blockWithLocation, combined.last()); }
From source file:org.apache.hadoop.hbase.regionserver.MemStore.java
/** * Given the specs of a column, update it, first by inserting a new record, * then removing the old one. Since there is only 1 KeyValue involved, the memstoreTS * will be set to 0, thus ensuring that they instantly appear to anyone. The underlying * store will ensure that the insert/delete each are atomic. A scanner/reader will either * get the new value, or the old value and all readers will eventually only see the new * value after the old was removed.//from w w w . j av a 2 s . com * * @param row * @param family * @param qualifier * @param newValue * @param now * @return Timestamp */ public long updateColumnValue(byte[] row, byte[] family, byte[] qualifier, long newValue, long now) { this.lock.readLock().lock(); try { KeyValue firstKv = KeyValue.createFirstOnRow(row, family, qualifier); // Is there a KeyValue in 'snapshot' with the same TS? If so, upgrade the timestamp a bit. SortedSet<KeyValue> snSs = snapshot.tailSet(firstKv); if (!snSs.isEmpty()) { KeyValue snKv = snSs.first(); // is there a matching KV in the snapshot? if (snKv.matchingRow(firstKv) && snKv.matchingQualifier(firstKv)) { if (snKv.getTimestamp() == now) { // poop, now += 1; } } } // logic here: the new ts MUST be at least 'now'. But it could be larger if necessary. // But the timestamp should also be max(now, mostRecentTsInMemstore) // so we cant add the new KV w/o knowing what's there already, but we also // want to take this chance to delete some kvs. So two loops (sad) SortedSet<KeyValue> ss = kvset.tailSet(firstKv); Iterator<KeyValue> it = ss.iterator(); while (it.hasNext()) { KeyValue kv = it.next(); // if this isnt the row we are interested in, then bail: if (!kv.matchingColumn(family, qualifier) || !kv.matchingRow(firstKv)) { break; // rows dont match, bail. } // if the qualifier matches and it's a put, just RM it out of the kvset. if (kv.getType() == KeyValue.Type.Put.getCode() && kv.getTimestamp() > now && firstKv.matchingQualifier(kv)) { now = kv.getTimestamp(); } } // create or update (upsert) a new KeyValue with // 'now' and a 0 memstoreTS == immediately visible return upsert(Arrays.asList(new KeyValue(row, family, qualifier, now, Bytes.toBytes(newValue)))); } finally { this.lock.readLock().unlock(); } }
From source file:org.wrml.runtime.rest.DefaultApiLoader.java
protected Object decipherDocumentSurrogateKeyValue(final URI uri, final Prototype prototype) { final ApiNavigator apiNavigator = getParentApiNavigator(uri); if (apiNavigator == null) { return null; }/*from ww w.j av a 2 s .co m*/ final SortedSet<Parameter> surrogateKeyComponents = apiNavigator.getSurrogateKeyComponents(uri, prototype); if (surrogateKeyComponents == null || surrogateKeyComponents.isEmpty()) { return null; } final Set<String> allKeySlotNames = prototype.getAllKeySlotNames(); final Object surrogateKeyValue; if (surrogateKeyComponents.size() == 1) { final Parameter surrogateKeyPair = surrogateKeyComponents.first(); final String slotName = surrogateKeyPair.getName(); if (!allKeySlotNames.contains(slotName)) { return null; } final String slotTextValue = surrogateKeyPair.getValue(); final Object slotValue = parseSlotValueSyntacticText(prototype, slotName, slotTextValue); surrogateKeyValue = slotValue; } else { final SortedMap<String, Object> keySlots = new TreeMap<String, Object>(); for (final Parameter surrogateKeyPair : surrogateKeyComponents) { final String slotName = surrogateKeyPair.getName(); if (!allKeySlotNames.contains(slotName)) { continue; } final String slotTextValue = surrogateKeyPair.getValue(); final Object slotValue = parseSlotValueSyntacticText(prototype, slotName, slotTextValue); if (slotValue == null) { continue; } keySlots.put(slotName, slotValue); } if (keySlots.size() == 1) { surrogateKeyValue = keySlots.get(keySlots.firstKey()); } else { surrogateKeyValue = new CompositeKey(keySlots); } } return surrogateKeyValue; }
From source file:org.libreplan.web.orders.ManageOrderElementAdvancesModel.java
@Override @Transactional(readOnly = true)/*from w w w .j a va2 s . c o m*/ public AdvanceMeasurement getLastAdvanceMeasurement(DirectAdvanceAssignment assignment) { if (assignment != null) { SortedSet<AdvanceMeasurement> advanceMeasurements = assignment.getAdvanceMeasurements(); if (advanceMeasurements.size() > 0) { return advanceMeasurements.first(); } } return null; }
From source file:org.wrml.runtime.rest.ApiNavigator.java
/** * Determine which resource(s) match the requested resource id. * <p/>//ww w . ja va2 s. com * Note: This needs to be as fast as possible because it is used during client request handling. */ private SortedSet<ResourceMatchResult> match(final URI uri) { ApiNavigator.LOG.debug("Attempting match on URI {}", new Object[] { uri }); if (uri == null) { ApiNavigator.LOG.error("3 This ApiNavigator cannot locate a resource with a *null* identifier."); throw new ApiNavigatorException("This ApiNavigator cannot locate a resource with a *null* identifier.", null, this); } final URI apiUri = getApiUri(); final String requestUriString = uri.toString(); final String apiUriString = apiUri.toString(); if (!requestUriString.startsWith(apiUriString)) { ApiNavigator.LOG.error("4 This ApiNavigator has charted \"" + apiUri + "\", which does not manage the specified resource (" + uri + ")"); throw new ApiNavigatorException( "This ApiNavigator has charted \"" + apiUri + "\", which does not manage the specified resource (" + uri + ")", null, this, Status.NOT_FOUND); } final String path = requestUriString.substring(apiUriString.length()); final SortedSet<ResourceMatchResult> results = matchPath(path); if (results == null || results.isEmpty() || results.size() == 1) { return results; } int resultsTiedForFirst = 0; final int highestScore = results.first().getScore(); for (final ResourceMatchResult result : results) { final int score = result.getScore(); if (score == highestScore) { resultsTiedForFirst++; } else { break; } } // TODO there's no differentiation here; either a lot of logic missing, or a lot of unnecessary logic if (resultsTiedForFirst == 1) { return results; } return results; }