@Override public boolean equals(Object obj) { if (obj == null) { return false; } if (!(obj instanceof Tuple<?, ?>)) { return false; } Tuple<?, ?> other = (Tuple<?, ?>) obj; return Objects.equal(first, other.getFirst()) && Objects.equal(second, other.getSecond()); }
/** * Extracts all <tt>first</tt> components of the given collection of tuples and returns them as list. * * @param tuples the collection of tuples to process * @param <T> the type of the tuples involved * @param <K> the type of the first elements of the tuples * @param <V> the type of the second elements of the tuples * @return a list containing each <tt>first</tt> component of the collection of given tuples. */ public static <T extends Tuple<K, V>, K, V> List<K> firsts(@Nonnull Collection<T> tuples) { List<K> result = new ArrayList<>(tuples.size()); for (Tuple<K, V> t : tuples) { result.add(t.getFirst()); } return result; }
protected PreparedStatement getStmt() throws SQLException { if (stmt == null) { if (retrieveGeneratedKeys) { stmt = c.prepareStatement(sb.toString(), Statement.RETURN_GENERATED_KEYS); } else { stmt = c.prepareStatement(sb.toString(), ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY); } for (Tuple<Integer, Object> t : parameters) { stmt.setObject(t.getFirst(), t.getSecond()); } } return stmt; }
private HttpHost makeHttpHost(Tuple<String, Integer> hostnameAndPort) { return new HttpHost(hostnameAndPort.getFirst(), hostnameAndPort.getSecond(), SCHEME_HTTP); }
@Override public String toString() { StringBuilder sb = new StringBuilder(); for (Tuple<Double, Double> location : data()) { sb.append(location.getFirst()); sb.append(","); sb.append(location.getSecond()); sb.append("\n"); } return sb.toString(); } }
/** * Fetches the entity of given type with the given id. * <p> * May use a global cache to load the entity. * * @param routing the routing info used to lookup the entity (might be <tt>null</tt> if no routing is required). * @param type the type of the desired entity * @param id the id of the desired entity * @param <E> the type of entities to fetch * @return the resolved entity (or <tt>null</tt> if not found) */ @Nullable public <E extends Entity> E fetchFromCache(@Nullable String routing, @Nonnull Class<E> type, @Nullable String id) { return fetch(routing, type, id).getFirst(); }
/** * Converts a collection of tuples into a map * * @param values the collection of tuples to be converted * @param <K> the key type of the map and therefore the type of the first component of the tuples * @param <V> the value type of the map and therefore the type of the second component of the tuples * @return a map containing an entry for each tuple in the collection, where the key is the first component of the * tuple and the value is the second component of the tuple. If two tuples have equal values as first * component, the specific map entry will be overridden in the order defined in the given collection. */ public static <K, V> Map<K, V> toMap(@Nonnull Collection<Tuple<K, V>> values) { Map<K, V> result = new HashMap<>(); for (Tuple<K, V> e : values) { result.put(e.getFirst(), e.getSecond()); } return result; }
@Override public String toString() { StringBuilder sb = new StringBuilder(); for (Tuple<String, String> e : getMDC()) { sb.append(e.getFirst()); sb.append(": "); sb.append(e.getSecond()); sb.append("\n"); } return sb.toString(); } }
/** * Fetches the entity wrapped in an {@link java.util.Optional} of given type with the given id. * <p> * May use a global cache to load the entity. * * @param routing the routing info used to lookup the entity (might be <tt>null</tt> if no routing is required). * @param type the type of the desired entity * @param id the id of the desired entity * @param <E> the type of entities to fetch * @return the resolved entity wrapped in an {@link java.util.Optional} (or {@link Optional#EMPTY} if not found) */ public <E extends Entity> Optional<E> fetchOptionalFromCache(@Nullable String routing, @Nonnull Class<E> type, @Nullable String id) { return Optional.ofNullable(fetch(routing, type, id).getFirst()); }
protected synchronized void initializeClient() { if (mongoClient != null) { return; } List<ServerAddress> hosts = Arrays.stream(dbHosts.split(",")) .map(String::trim) .map(hostname -> PortMapper.mapPort(SERVICE_NAME, hostname, MONGO_PORT)) .map(hostAndPort -> new ServerAddress(hostAndPort.getFirst(), hostAndPort.getSecond())) .filter(Objects::nonNull) .collect(Collectors.toList()); mongoClient = new MongoClient(hosts); createIndices(mongoClient.getDatabase(dbName)); }
/** * Returns the filename without its file extension. * <p> * The file extension is everything after the last <tt>.</tt> in the filename. * * @param path the path to a file to parse * @return the filename without extension or <tt>null</tt> if the given path is empty, <tt>null</tt> or does not * have a filename (ends with a sepratator). */ @Nullable public static String getFilenameWithoutExtension(@Nullable String path) { return Strings.splitAtLast(getFilenameAndExtension(path), ".").getFirst(); }
/** * Merges the given multi map into this one. * <p> * If both maps contain values for the same key, to lists will be joined together. * <p> * <b>Note</b>: This will modify the callee instead of creating a new result map * * @param other the other map to merge into this one * @return the callee itself for further processing */ public MultiMap<K, V> merge(MultiMap<K, V> other) { if (other != null) { other.base.entrySet().stream().flatMap(Tuple::flatten).forEach(t -> put(t.getFirst(), t.getSecond())); } return this; }
@Override protected Tuple<Double, Double> copyValue(Tuple<Double, Double> value) { return Tuple.create(value.getFirst(), value.getSecond()); }
private Tuple<String, Integer> mapPort(Tuple<String, Integer> hostAndPort) { Tuple<String, Integer> effectiveHostAndPort = PortMapper.mapPort(SERVICE_ELASTICSEARCH, hostAndPort.getFirst(), hostAndPort.getSecond()); if (!Objects.equals(effectiveHostAndPort.getSecond(), hostAndPort.getSecond())) { dockerDetected = true; } return effectiveHostAndPort; }
private void orderBy(Compiler compiler) { if (!orderBys.isEmpty()) { compiler.getWHEREBuilder().append(" ORDER BY "); Monoflop mf = Monoflop.create(); for (Tuple<Mapping, Boolean> e : orderBys) { if (mf.successiveCall()) { compiler.getWHEREBuilder().append(", "); } compiler.getWHEREBuilder().append(compiler.translateColumnName(e.getFirst())); compiler.getWHEREBuilder().append(e.getSecond() ? " ASC" : " DESC"); } } }
private Tuple<String, Integer> parsePort(Tuple<String, String> hostnameAndPort) { if (Strings.isFilled(hostnameAndPort.getSecond())) { try { return Tuple.create(hostnameAndPort.getFirst(), Integer.parseInt(hostnameAndPort.getSecond())); } catch (NumberFormatException e) { Exceptions.handle() .to(LOG) .withSystemErrorMessage("Invalid port in 'elasticsearch.hosts': %s %s", hostnameAndPort.getFirst(), hostnameAndPort.getSecond()) .handle(); } } return Tuple.create(hostnameAndPort.getFirst(), DEFAULT_HTTP_PORT); }
/** * Translates a column name into an effective name by applying aliases and rewrites. * * @param column the column to translate * @return the translated name which is used in the database */ public String translateColumnName(Mapping column) { Tuple<String, EntityDescriptor> aliasAndDescriptor = determineAlias(column.getParent()); EntityDescriptor effectiveDescriptor = aliasAndDescriptor.getSecond(); if (effectiveDescriptor != null) { return aliasAndDescriptor.getFirst() + "." + effectiveDescriptor.rewritePropertyName(column.getName()); } else { return aliasAndDescriptor.getFirst() + "." + column.getName(); } }
private void appendCanonicalQueryString(WebContext ctx, StringBuilder canonicalRequest) { QueryStringDecoder qsd = new QueryStringDecoder(ctx.getRequest().uri(), Charsets.UTF_8); List<Tuple<String, List<String>>> queryString = Tuple.fromMap(qsd.parameters()); queryString.sort(Comparator.comparing(Tuple::getFirst)); Monoflop mf = Monoflop.create(); for (Tuple<String, List<String>> param : queryString) { if (param.getSecond().isEmpty()) { appendQueryStringValue(param.getFirst(), "", canonicalRequest, mf.successiveCall()); } else { for (String value : param.getSecond()) { appendQueryStringValue(param.getFirst(), value, canonicalRequest, mf.successiveCall()); } } } canonicalRequest.append("\n"); }
@Override public Callback<WebContext> preparePreDispatch(WebContext ctx) { String uri = getEffectiveURI(ctx); Tuple<String, String> bucketAndObject = Strings.split(uri, "/"); if (Strings.isEmpty(bucketAndObject.getSecond())) { return null; } Bucket bucket = storage.getBucket(bucketAndObject.getFirst()); if (!bucket.exists() && !storage.isAutocreateBuckets()) { return null; } InputStreamHandler handler = createInputStreamHandler(ctx); ctx.setContentHandler(handler); return req -> writeObject(req, bucketAndObject.getFirst(), bucketAndObject.getSecond(), handler); }
protected QueryBuilder parseToken(LookaheadReader reader) { if (reader.current().is('(')) { return parseTokenInBrackets(reader); } boolean negate = checkIfNegated(reader); Tuple<String, String> fieldAndValue = parseFieldAndValue(reader); if (Strings.isEmpty(fieldAndValue.getSecond())) { return null; } QueryBuilder qry = compileToken(fieldAndValue.getFirst(), fieldAndValue.getSecond()); if (qry != null && negate) { return QueryBuilders.boolQuery().mustNot(qry); } else { return qry; } }