List of usage examples for org.apache.commons.lang3.tuple Pair getLeft
public abstract L getLeft();
Gets the left element from this pair.
When treated as a key-value pair, this is the key.
From source file:com.streamsets.pipeline.stage.BaseHiveIT.java
/** * Validate structure of the result set (column names and types). */// ww w . ja v a 2 s . c o m public static void assertResultSetStructure(ResultSet rs, Pair<String, Integer>... columns) throws Exception { ResultSetMetaData metaData = rs.getMetaData(); Assert.assertEquals(Utils.format("Unexpected number of columns"), columns.length, metaData.getColumnCount()); int i = 1; for (Pair<String, Integer> column : columns) { Assert.assertEquals(Utils.format("Unexpected name for column {}", i), column.getLeft(), metaData.getColumnName(i)); Assert.assertEquals(Utils.format("Unexpected type for column {}", i), (int) column.getRight(), metaData.getColumnType(i)); i++; } }
From source file:forge.card.BoosterGenerator.java
public static List<PaperCard> getBoosterPack(SealedProduct.Template template) { List<PaperCard> result = new ArrayList<>(); List<PrintSheet> sheetsUsed = new ArrayList<>(); CardEdition edition = StaticData.instance().getEditions().get(template.getEdition()); boolean hasFoil = edition != null && !template.getSlots().isEmpty() && MyRandom.getRandom().nextDouble() < edition.getFoilChanceInBooster() && edition.getFoilType() != FoilType.NOT_SUPPORTED; boolean foilAtEndOfPack = hasFoil && edition.getFoilAlwaysInCommonSlot(); String foilSlot = !hasFoil ? null : foilAtEndOfPack ? BoosterSlots.COMMON : Aggregates.random(template.getSlots()).getKey(); String extraFoilSheetKey = edition != null ? edition.getAdditionalSheetForFoils() : ""; for (Pair<String, Integer> slot : template.getSlots()) { String slotType = slot.getLeft(); // add expansion symbol here? int numCards = slot.getRight(); String[] sType = TextUtil.splitWithParenthesis(slotType, ' '); String setCode = sType.length == 1 && template.getEdition() != null ? template.getEdition() : null; String sheetKey = StaticData.instance().getEditions().contains(setCode) ? slotType.trim() + " " + setCode : slotType.trim();/* ww w . j a v a 2s .co m*/ boolean foilInThisSlot = hasFoil && slotType.startsWith(foilSlot); if (foilInThisSlot) numCards--; PrintSheet ps = getPrintSheet(sheetKey); result.addAll(ps.random(numCards, true)); sheetsUsed.add(ps); if (foilInThisSlot && !foilAtEndOfPack) { if (!extraFoilSheetKey.isEmpty()) { // TODO: extra foil sheets are currently reliably supported only for boosters with FoilAlwaysInCommonSlot=True. // If FoilAlwaysInCommonSlot is false, a card from the extra sheet may still replace a card in any slot. List<PaperCard> foilCards = new ArrayList<>(); for (PaperCard card : ps.toFlatList()) { if (!foilCards.contains(card)) { foilCards.add(card); } } addCardsFromExtraSheet(foilCards, sheetKey); result.add(generateFoilCard(foilCards)); } else { result.add(generateFoilCard(ps)); } } } if (hasFoil && foilAtEndOfPack) { List<PaperCard> foilCards = new ArrayList<>(); for (PrintSheet printSheet : sheetsUsed) { for (PaperCard card : printSheet.toFlatList()) { if (!foilCards.contains(card)) { foilCards.add(card); } } } if (!extraFoilSheetKey.isEmpty()) { addCardsFromExtraSheet(foilCards, extraFoilSheetKey); } result.add(generateFoilCard(foilCards)); } return result; }
From source file:com.acmutv.ontoqa.core.parser.SimpleSltagParser.java
private static void processAdjunctions(ParserState dashboard) throws LTAGException { List<String> words = dashboard.getWords(); Sltag curr = dashboard.getCurr();/*from w w w . j a v a2s. c om*/ Map<Integer, Triple<Variable, Variable, Set<Statement>>> missedMainVariables = dashboard .getMissedMainVariables(); Iterator<Pair<Sltag, Integer>> waitingAdjunctions = dashboard.getAdjunctions().iterator(); while (waitingAdjunctions.hasNext()) { Pair<Sltag, Integer> entry = waitingAdjunctions.next(); Sltag toAdjunct = entry.getLeft(); Integer start = entry.getRight(); String startLexicalEntry = (start != null) ? words.get(start) : null; LtagNode localTarget = curr.firstMatch(toAdjunct.getRoot().getCategory(), startLexicalEntry, null); if (localTarget != null) { /* CAN MAKE ADJUNCTION */ if (curr.getSemantics().getMainVariable() == null && toAdjunct.isLeftAdj() && missedMainVariables.containsKey(start)) { /* INSPECT MAIN VARIABLE MISS */ int lookup = (start != null) ? start : 0; Variable missedMainVar = missedMainVariables.get(lookup).getMiddle(); LOGGER.warn("Found possible main variable miss at pos {}: {}", lookup, missedMainVar); curr.getSemantics().setMainVariable(missedMainVar); LOGGER.warn("Main variable temporarily set to: {}", missedMainVar); curr.adjunction(toAdjunct, localTarget); curr.getSemantics().setMainVariable(null); LOGGER.warn("Resetting main variable to NULL"); } else if (curr.getSemantics().getMainVariable() == null && toAdjunct.isRightAdj() && missedMainVariables.containsKey((start != null) ? start + 2 : 1)) { int lookup = (start != null) ? start + 2 : 1; Variable missedMainVar = missedMainVariables.get(lookup).getMiddle(); LOGGER.warn("Found possible main variable miss at pos {}: {}", lookup, missedMainVar); curr.getSemantics().setMainVariable(missedMainVar); LOGGER.warn("Main variable temporarily set to: {}", missedMainVar); curr.adjunction(toAdjunct, localTarget); curr.getSemantics().setMainVariable(null); LOGGER.warn("Resetting main variable to NULL"); } else { curr.adjunction(toAdjunct, localTarget); } LOGGER.debug("Adjuncted {} on {}", toAdjunct.toPrettyString(), localTarget); waitingAdjunctions.remove(); } } }
From source file:com.etsy.arbiter.workflow.WorkflowGraphBuilder.java
/** * Processes all connected subcomponents of a given graph * * @param parentGraph The graph for which to process subcomponents * @return A Triple with these elements - A new graph with fork/join pairs inserted, the "first" node in this graph, and the "last" node in this graph * @throws WorkflowGraphException/*from w ww . j av a 2 s. co m*/ * @throws DirectedAcyclicGraph.CycleFoundException */ private static Triple<DirectedAcyclicGraph<Action, DefaultEdge>, Action, Action> processSubcomponents( DirectedAcyclicGraph<Action, DefaultEdge> parentGraph) throws WorkflowGraphException, DirectedAcyclicGraph.CycleFoundException { ConnectivityInspector<Action, DefaultEdge> inspector = new ConnectivityInspector<>(parentGraph); List<Set<Action>> connectedComponents = inspector.connectedSets(); // Recursively process each connected subcomponent of the graph List<DirectedAcyclicGraph<Action, DefaultEdge>> componentGraphs = new ArrayList<>( connectedComponents.size()); for (Set<Action> subComponent : connectedComponents) { componentGraphs.add(buildComponentGraph(subComponent, parentGraph)); } DirectedAcyclicGraph<Action, DefaultEdge> result = new DirectedAcyclicGraph<>(DefaultEdge.class); for (DirectedAcyclicGraph<Action, DefaultEdge> subSubgraph : componentGraphs) { Graphs.addGraph(result, subSubgraph); } // If we have more than one subcomponent, we must insert a fork/join to run them in parallel if (componentGraphs.size() > 1) { Pair<Action, Action> forkJoin = addForkJoin(result); Action fork = forkJoin.getLeft(); Action join = forkJoin.getRight(); for (DirectedAcyclicGraph<Action, DefaultEdge> subSubgraph : componentGraphs) { for (Action vertex : subSubgraph.vertexSet()) { // Vertices with no incoming edges attach directly to the fork if (subSubgraph.inDegreeOf(vertex) == 0) { result.addDagEdge(fork, vertex); } // Vertices with no outgoing edges attach directly to the join if (subSubgraph.outDegreeOf(vertex) == 0) { result.addDagEdge(vertex, join); } } } } // The graph will now have one node with no outgoing edges and one node with no incoming edges // The node with no outgoing edges is the "last" node in the resulting graph // The node with no incoming edges is the "first" node in the resulting graph // These are pulled out specifically to make it easier to attach the resulting graph into another one Action noOutgoing = null; Action noIncoming = null; for (Action vertex : result.vertexSet()) { if (noIncoming == null && result.inDegreeOf(vertex) == 0) { noIncoming = vertex; } } for (Action vertex : result.vertexSet()) { if (noOutgoing == null && result.outDegreeOf(vertex) == 0) { noOutgoing = vertex; } } return Triple.of(result, noIncoming, noOutgoing); }
From source file:com.twitter.graphjet.bipartite.edgepool.EdgePoolConcurrentTestHelper.java
/** * This helper method sets up a concurrent read-write situation with a single writer and multiple * readers that access the same underlying edgePool, and tests for correct edge access during * simultaneous edge writes. This helps test read consistency during arbitrary points of * inserting edges. Note that the exact read-write sequence here is non-deterministic and would * vary depending on the machine, but the hope is that given the large number of readers the reads * would be done at many different points of edge insertion. The test itself checks only for * partial correctness (it could have false positives) so this should only be used as a supplement * to other testing.//ww w. jav a 2 s.com * * @param edgePool is the underlying * {@link com.twitter.graphjet.bipartite.edgepool.EdgePool} * @param numReadersPerNode is the number of reader threads to use per node * @param leftSize is the number of left nodes * @param rightSize is the number of right nodes * @param edgeProbability is the probability of an edge between a left-right node pair * @param random is the random number generator to use for generating a random graph */ public static void testRandomConcurrentReadWriteThreads(EdgePool edgePool, int numReadersPerNode, int leftSize, int rightSize, double edgeProbability, Random random) { int maxWaitingTimeForThreads = 20; // in milliseconds int numReaders = leftSize * numReadersPerNode; CountDownLatch readersDoneLatch = new CountDownLatch(numReaders); // First, construct a random set of edges to insert in the graph Set<Pair<Integer, Integer>> edges = Sets .newHashSetWithExpectedSize((int) (leftSize * rightSize * edgeProbability)); List<EdgePoolReader> readers = Lists.newArrayListWithCapacity(numReaders); Int2ObjectMap<IntSet> leftSideGraph = new Int2ObjectOpenHashMap<IntSet>(leftSize); int averageLeftDegree = (int) (rightSize * edgeProbability); for (int i = 0; i < leftSize; i++) { IntSet nodeEdges = new IntOpenHashSet(averageLeftDegree); for (int j = 0; j < rightSize; j++) { if (random.nextDouble() < edgeProbability) { nodeEdges.add(j); edges.add(Pair.of(i, j)); } } leftSideGraph.put(i, nodeEdges); } // Create a bunch of leftReaders per node that'll read from the graph at random for (int i = 0; i < leftSize; i++) { for (int j = 0; j < numReadersPerNode; j++) { readers.add(new EdgePoolReader(edgePool, new CountDownLatch(0), readersDoneLatch, i, random.nextInt(maxWaitingTimeForThreads))); } } // Create a single writer that will insert these edges in random order List<WriterInfo> writerInfo = Lists.newArrayListWithCapacity(edges.size()); List<Pair<Integer, Integer>> edgesList = Lists.newArrayList(edges); Collections.shuffle(edgesList); CountDownLatch writerDoneLatch = new CountDownLatch(edgesList.size()); for (Pair<Integer, Integer> edge : edgesList) { writerInfo.add(new WriterInfo(edge.getLeft(), edge.getRight(), new CountDownLatch(0), writerDoneLatch)); } ExecutorService executor = Executors.newFixedThreadPool(numReaders + 1); // single writer List<Callable<Integer>> allThreads = Lists.newArrayListWithCapacity(numReaders + 1); // First, we add the writer allThreads.add(Executors.callable(new EdgePoolWriter(edgePool, writerInfo), 1)); // then the readers for (int i = 0; i < numReaders; i++) { allThreads.add(Executors.callable(readers.get(i), 1)); } // these will execute in some non-deterministic order Collections.shuffle(allThreads, random); // Wait for all the processes to finish try { List<Future<Integer>> results = executor.invokeAll(allThreads, 10, TimeUnit.SECONDS); for (Future<Integer> result : results) { assertTrue(result.isDone()); assertEquals(1, result.get().intValue()); } } catch (InterruptedException e) { throw new RuntimeException("Execution for a thread was interrupted: ", e); } catch (ExecutionException e) { throw new RuntimeException("Execution issue in an executor thread: ", e); } // confirm that these worked as expected try { readersDoneLatch.await(); writerDoneLatch.await(); } catch (InterruptedException e) { throw new RuntimeException("Execution for last reader was interrupted: ", e); } // Check that all readers' read info is consistent with the graph for (EdgePoolReader reader : readers) { IntSet expectedEdges = leftSideGraph.get(reader.queryNode); assertTrue(reader.getQueryNodeDegree() <= expectedEdges.size()); if (reader.getQueryNodeDegree() == 0) { assertNull(reader.getQueryNodeEdges()); } else { for (int edge : reader.getQueryNodeEdges()) { assertTrue(expectedEdges.contains(edge)); } } } }
From source file:com.etsy.arbiter.workflow.WorkflowGraphBuilder.java
/** * Recursively insert fork/joins for connected subcomponents of a graph * * @param vertices The set of vertices to process * @param parentGraph The parentGraph graph of these vertices * @return DirectedAcyclicGraph A new graph containing all the given vertices with appropriate fork/join pairs inserted * @throws WorkflowGraphException/* www. j a va 2 s . c o m*/ * @throws DirectedAcyclicGraph.CycleFoundException */ private static DirectedAcyclicGraph<Action, DefaultEdge> buildComponentGraph(Set<Action> vertices, DirectedAcyclicGraph<Action, DefaultEdge> parentGraph) throws WorkflowGraphException, DirectedAcyclicGraph.CycleFoundException { DirectedAcyclicGraph<Action, DefaultEdge> subgraph = buildSubgraph(parentGraph, vertices); // Start by pulling out the vertices with no incoming edges // These can run in parallel in a fork-join Set<Action> initialNodes = new HashSet<>(); for (Action vertex : subgraph.vertexSet()) { if (subgraph.inDegreeOf(vertex) == 0) { initialNodes.add(vertex); } } DirectedAcyclicGraph<Action, DefaultEdge> result = new DirectedAcyclicGraph<>(DefaultEdge.class); if (initialNodes.isEmpty()) { // This is a very odd case, but just in case we'll fail if it happens throw new WorkflowGraphException("No nodes with inDegree = 0 found. This shouldn't happen."); } else if (initialNodes.size() == 1) { // If there is only one node, we can't put it in a fork/join // In this case we'll add just that vertex to the resulting graph Action vertex = initialNodes.iterator().next(); result.addVertex(vertex); // Remove the processed vertex so that we have new unprocessed subcomponents subgraph.removeVertex(vertex); } else { // If there are multiple nodes, insert a fork/join pair to run them in parallel Pair<Action, Action> forkJoin = addForkJoin(result); Action fork = forkJoin.getLeft(); Action join = forkJoin.getRight(); for (Action vertex : initialNodes) { result.addVertex(vertex); result.addDagEdge(fork, vertex); result.addDagEdge(vertex, join); // Remove the processed vertex so that we have new unprocessed subcomponents subgraph.removeVertex(vertex); } } // Now recursively process the graph with the processed nodes removed Triple<DirectedAcyclicGraph<Action, DefaultEdge>, Action, Action> subComponentGraphTriple = processSubcomponents( subgraph); DirectedAcyclicGraph<Action, DefaultEdge> subComponentGraph = subComponentGraphTriple.getLeft(); // Having processed the subcomponents, we attach the "last" node of the graph created here to // the "first" node of the subcomponent graph Action noIncoming = subComponentGraphTriple.getMiddle(); Action noOutgoing = null; for (Action vertex : result.vertexSet()) { if (noOutgoing == null && result.outDegreeOf(vertex) == 0) { noOutgoing = vertex; } } Graphs.addGraph(result, subComponentGraph); if (noOutgoing != null && noIncoming != null && !noOutgoing.equals(noIncoming)) { result.addDagEdge(noOutgoing, noIncoming); } return result; }
From source file:com.joyent.manta.http.HttpDownloadContinuationMarker.java
/** * Builds a download marker after confirming that the initial response matches any hints that the initial request * supplied. If a hint is missing it will not be checked. If the response * * @param requestHints etag and range from initial request (If-Match and Range) * @param responseFingerprint etag and range from initial response (ETag and Content-Range) * @return a marker which can be used to track download progress and verify future responses * @throws ProtocolException thrown when a hint is provided but not satisfied */// ww w .ja v a2 s . c o m static HttpDownloadContinuationMarker validateInitialExchange(final Pair<String, Request> requestHints, final int responseCode, final Pair<String, Response> responseFingerprint) throws ProtocolException { // there was an if-match header and the response etag does not match if (requestHints.getLeft() != null && !requestHints.getLeft().equals(responseFingerprint.getLeft())) { throw new ProtocolException(String.format("ETag does not match If-Match: If-Match [%s], ETag [%s]", requestHints.getLeft(), responseFingerprint.getLeft())); } final boolean rangeRequest = requestHints.getRight() != null; // there was a request range and an invalid response range (or none) was returned // Note: we should use the more complete range (the response range) to invoke match so as many values are // compared as possible if (rangeRequest && !requestHints.getRight().matches(responseFingerprint.getRight())) { throw new ProtocolException( String.format("Content-Range does not match Request range: Range [%s], Content-Range [%s]", requestHints.getRight(), responseFingerprint.getRight())); } // if there was a request range the response code should be 206 if (rangeRequest && responseCode != SC_PARTIAL_CONTENT) { throw new ProtocolException( String.format("Unexpected response code for range request: expected [%d], got [%d]", SC_PARTIAL_CONTENT, responseCode)); } // if there was no request range the response code should be 200 if (!rangeRequest && responseCode != SC_OK) { throw new ProtocolException( String.format("Unexpected response code for non-range request: expected [%d], got [%d]", SC_OK, responseCode)); } return new HttpDownloadContinuationMarker(responseFingerprint.getLeft(), responseFingerprint.getRight()); }
From source file:de.tntinteractive.portalsammler.engine.SecureStore.java
private static void readIndex(final StorageLayer storage, final SecureRandom srand, final byte[] key, final SecureStore ret) throws IOException { final InputStream stream = storage.openInputStream("index"); try {//w w w . j av a2s. co m final Pair<Integer, MapReader> saltAndReader = createMapReader(stream, srand, key); ret.indexSalt = saltAndReader.getLeft(); final MapReader r = saltAndReader.getRight(); Pair<String, Map<String, String>> p; while ((p = r.readNext()) != null) { final DocumentInfo di = DocumentInfo.parse(p.getLeft()); ret.index.putDocument(di, p.getRight()); } r.close(); } finally { stream.close(); } }
From source file:de.tntinteractive.portalsammler.engine.SecureStore.java
private static void readSettings(final StorageLayer storage, final SecureRandom srand, final byte[] key, final SecureStore ret) throws IOException { final InputStream stream = storage.openInputStream("meta"); try {/*from w w w . java 2s. c o m*/ final Pair<Integer, MapReader> saltAndReader = createMapReader(stream, srand, key); ret.settingSalt = saltAndReader.getLeft(); final MapReader r = saltAndReader.getRight(); Pair<String, Map<String, String>> p; while ((p = r.readNext()) != null) { final SourceSettings s = new SourceSettings(p.getRight()); ret.getSettings().putSettings(p.getLeft(), s); } r.close(); } finally { stream.close(); } }
From source file:com.twitter.distributedlog.service.DistributedLogServer.java
static void closeServer(Pair<DistributedLogServiceImpl, Server> pair, long gracefulShutdownPeriod, TimeUnit timeUnit) {// ww w . j a v a 2 s . c o m if (null != pair.getLeft()) { pair.getLeft().shutdown(); if (gracefulShutdownPeriod > 0) { try { timeUnit.sleep(gracefulShutdownPeriod); } catch (InterruptedException e) { logger.info("Interrupted on waiting service shutting down state propagated to all clients : ", e); } } } if (null != pair.getRight()) { logger.info("Closing dl thrift server."); pair.getRight().close(); logger.info("Closed dl thrift server."); } }