List of usage examples for org.apache.commons.lang3.tuple Pair getKey
@Override public final L getKey()
Gets the key from this pair.
This method implements the Map.Entry interface returning the left element as the key.
From source file:com.trenako.utility.PeriodUtilsTests.java
@Test public void shouldCalculatePeriodForMonths() { int numberOfMonths = 11; DateTime start = new DateTime(2010, 1, 1, 10, 30); DateTime end = start.plusMonths(numberOfMonths); Pair<String, Integer> monthPair = PeriodUtils.period(start, end); assertEquals(MONTHS_LABEL, monthPair.getKey()); assertEquals(numberOfMonths, (int) monthPair.getValue()); }
From source file:com.trenako.utility.PeriodUtilsTests.java
@Test public void shouldCalculatePeriodForOneMinute() { int numberOfMinutes = 1; DateTime start = new DateTime(2010, 1, 1, 10, 30); DateTime end = start.plusMinutes(numberOfMinutes); Pair<String, Integer> minutePair = PeriodUtils.period(start, end); assertEquals(MINUTE_LABEL, minutePair.getKey()); assertEquals(numberOfMinutes, (int) minutePair.getValue()); }
From source file:com.snaplogic.snaps.lunex.RequestProcessor.java
public String execute(RequestBuilder rBuilder) throws MalformedURLException, IOException { try {//from w w w . ja v a2 s . c o m URL api_url = new URL(rBuilder.getURL()); HttpURLConnection httpUrlConnection = (HttpURLConnection) api_url.openConnection(); httpUrlConnection.setRequestMethod(rBuilder.getMethod().toString()); httpUrlConnection.setDoInput(true); httpUrlConnection.setDoOutput(true); if (rBuilder.getSnapType() != LunexSnaps.Read) { rBuilder.getHeaders().add(Pair.of(CONTENT_LENGTH, rBuilder.getRequestBodyLenght())); } for (Pair<String, String> header : rBuilder.getHeaders()) { if (!StringUtils.isEmpty(header.getKey()) && !StringUtils.isEmpty(header.getValue())) { httpUrlConnection.setRequestProperty(header.getKey(), header.getValue()); } } log.debug(String.format(LUNEX_HTTP_INFO, rBuilder.getSnapType(), rBuilder.getURL(), httpUrlConnection.getRequestProperties().toString())); if (rBuilder.getSnapType() != LunexSnaps.Read) { String paramsJson = null; if (!StringUtils.isEmpty(paramsJson = rBuilder.getRequestBody())) { DataOutputStream cgiInput = new DataOutputStream(httpUrlConnection.getOutputStream()); log.debug(String.format(LUNEX_HTTP_REQ_INFO, paramsJson)); cgiInput.writeBytes(paramsJson); cgiInput.flush(); IOUtils.closeQuietly(cgiInput); } } List<String> input = null; StringBuilder response = new StringBuilder(); try (InputStream iStream = httpUrlConnection.getInputStream()) { input = IOUtils.readLines(iStream); } catch (IOException ioe) { log.warn(String.format(INPUT_STREAM_ERROR, ioe.getMessage())); try (InputStream eStream = httpUrlConnection.getErrorStream()) { if (eStream != null) { input = IOUtils.readLines(eStream); } else { response.append(String.format(INPUT_STREAM_ERROR, ioe.getMessage())); } } catch (IOException ioe1) { log.warn(String.format(INPUT_STREAM_ERROR, ioe1.getMessage())); } } statusCode = httpUrlConnection.getResponseCode(); log.debug(String.format(HTTP_STATUS, statusCode)); if (input != null && !input.isEmpty()) { for (String line : input) { response.append(line); } } return formatResponse(response, rBuilder); } catch (MalformedURLException me) { log.error(me.getMessage(), me); throw me; } catch (IOException ioe) { log.error(ioe.getMessage(), ioe); throw ioe; } catch (Exception ex) { log.error(ex.getMessage(), ex); throw ex; } }
From source file:com.trenako.utility.PeriodUtilsTests.java
@Test public void shouldCalculatePeriodForMinutes() { int numberOfMinutes = 53; DateTime start = new DateTime(2010, 1, 1, 10, 30); DateTime end = start.plusMinutes(numberOfMinutes); Pair<String, Integer> minutePair = PeriodUtils.period(start, end); assertEquals(MINUTES_LABEL, minutePair.getKey()); assertEquals(numberOfMinutes, (int) minutePair.getValue()); }
From source file:io.pravega.controller.store.stream.tables.TableHelper.java
/** * Find history record from the event when the given segment was sealed. * If segment is never sealed this method returns an empty list. * If segment is yet to be created, this method still returns empty list. * <p>//from w w w . j av a 2s . c o m * Find index that corresponds to segment start event. * Perform binary search on index+history records to find segment seal event. * <p> * If index table is not up to date we may have two cases: * 1. Segment create time > highest event time in index * 2. Segment seal time > highest event time in index * <p> * For 1 we cant have any searches in index and will need to fall through * History table starting from last indexed record. * <p> * For 2, fall through History Table starting from last indexed record * to find segment sealed event in history table. * * @param segment segment * @param indexTable index table * @param historyTable history table * @return */ public static List<Integer> findSegmentSuccessorCandidates(final Segment segment, final byte[] indexTable, final byte[] historyTable) { // fetch segment start time from segment Is // fetch last index Ic // fetch record corresponding to Ic. If segment present in that history record, fall through history table // else perform binary searchIndex // Note: if segment is present at Ic, we will fall through in the history table one record at a time Pair<Integer, Optional<IndexRecord>> search = IndexRecord.search(segment.getStart(), indexTable); final Optional<IndexRecord> recordOpt = search.getValue(); final int startingOffset = recordOpt.isPresent() ? recordOpt.get().getHistoryOffset() : 0; final Optional<HistoryRecord> historyRecordOpt = findSegmentCreatedEvent(startingOffset, segment, historyTable); // segment information not in history table if (!historyRecordOpt.isPresent()) { return new ArrayList<>(); } final int lower = search.getKey() / IndexRecord.INDEX_RECORD_SIZE; final int upper = (indexTable.length - IndexRecord.INDEX_RECORD_SIZE) / IndexRecord.INDEX_RECORD_SIZE; // index table may be stale, whereby we may not find segment.start to match an entry in the index table final Optional<IndexRecord> indexRecord = IndexRecord.readLatestRecord(indexTable); // if nothing is indexed read the first record in history table, hence offset = 0 final int lastIndexedRecordOffset = indexRecord.isPresent() ? indexRecord.get().getHistoryOffset() : 0; final Optional<HistoryRecord> lastIndexedRecord = HistoryRecord.readRecord(historyTable, lastIndexedRecordOffset, false); // if segment is present in history table but its offset is greater than last indexed record, // we cant do anything on index table, fall through. OR // if segment exists at the last indexed record in history table, fall through, // no binary search possible on index if (lastIndexedRecord.get().getScaleTime() < historyRecordOpt.get().getScaleTime() || lastIndexedRecord.get().getSegments().contains(segment.getNumber())) { // segment was sealed after the last index entry HistoryRecord startPoint = lastIndexedRecord.get().getScaleTime() < historyRecordOpt.get() .getScaleTime() ? historyRecordOpt.get() : lastIndexedRecord.get(); Optional<HistoryRecord> next = HistoryRecord.fetchNext(startPoint, historyTable, false); while (next.isPresent() && next.get().getSegments().contains(segment.getNumber())) { startPoint = next.get(); next = HistoryRecord.fetchNext(startPoint, historyTable, false); } if (next.isPresent()) { return next.get().getSegments(); } else { // we have reached end of history table which means segment was never sealed return new ArrayList<>(); } } else { // segment is definitely sealed and segment sealed event is also present in index table // we should be able to find it by doing binary search on Index table final Optional<HistoryRecord> record = findSegmentSealedEvent(lower, upper, segment.getNumber(), indexTable, historyTable); return record.isPresent() ? record.get().getSegments() : new ArrayList<>(); } }
From source file:code.elix_x.excore.utils.net.packets.SmartNetworkWrapper.java
public <REQ extends IMessage> void registerMessage3(final Function<REQ, Runnable> onReceive, Class<REQ> requestMessageType, Side side) { registerMessage(new Function<Pair<REQ, MessageContext>, Pair<Runnable, IMessage>>() { @Override/*w w w . j a v a 2 s. co m*/ public Pair<Runnable, IMessage> apply(Pair<REQ, MessageContext> t) { return new ImmutablePair<Runnable, IMessage>(onReceive.apply(t.getKey()), null); } }, requestMessageType, side); }
From source file:com.acmutv.ontoqa.benchmark.basic.QuestionB03Test.java
/** * Tests the question-answering with parsing. * @throws QuestionException when the question is malformed. * @throws OntoqaFatalException when the question cannot be processed due to some fatal errors. *//*from ww w. ja v a 2 s .c o m*/ @Test public void test_nlp_wired() throws Exception { final Grammar grammar = CommonGrammar.build_completeGrammar(); final Ontology ontology = Common.getOntology(); final Pair<Query, Answer> result = CoreController.process(QUESTION, grammar, ontology); final Query query = result.getKey(); final Answer answer = result.getValue(); LOGGER.info("Query: {}", query); LOGGER.info("Answer: {}", answer); Assert.assertTrue(QUERY_2.equals(query.toString()) || QUERY_2_bis.equals(query.toString())); Assert.assertEquals(ANSWER, answer); }
From source file:net.community.chest.gitcloud.facade.AbstractContextInitializer.java
protected void showArtifactsVersions() { scanArtifactsManifests(new Predicate<Pair<URL, Manifest>>() { @Override/* w ww.j a v a 2 s .c o m*/ @SuppressWarnings("synthetic-access") public boolean evaluate(Pair<URL, Manifest> e) { URL url = e.getKey(); Manifest manifest = e.getValue(); Pair<Attributes.Name, String> versionInfo = ManifestUtils.findFirstManifestAttributeValue(manifest, ManifestUtils.STANDARD_VERSION_ATTRS_NAMES); if (versionInfo == null) { logger.info( "showArtifactsVersions(" + url.toExternalForm() + ") no version information extracted"); } else { logger.info("showArtifactsVersions(" + url.toExternalForm() + ")[" + versionInfo.getLeft() + "]: " + versionInfo.getRight()); } return false; // don't stop } }); }
From source file:com.dangdang.config.service.zookeeper.ZookeeperConfigGroup.java
void reloadKey(final String nodePath) { try {//from w w w.ja va 2 s .co m Pair<String, String> keyValue = loadKey(nodePath); if (keyValue != null) { super.put(keyValue.getKey(), keyValue.getValue()); } } catch (Exception e) { throw Throwables.propagate(e); } }
From source file:com.hortonworks.registries.storage.impl.jdbc.provider.sql.statement.PreparedStatementBuilder.java
private void setStorableUpdatePreparedStatement(AbstractStorableUpdateQuery updateQuery) throws SQLException { List<Pair<Schema.Field, Object>> bindings = updateQuery.getBindings(); for (int i = 0; i < bindings.size(); i++) { Pair<Schema.Field, Object> binding = bindings.get(i); Schema.Type javaType = binding.getKey().getType(); storageDataTypeContext.setPreparedStatementParams(preparedStatement, javaType, i + 1, binding.getValue());// ww w . ja v a 2 s. co m } }