@Override public SmartQuery<E> orderAsc(Mapping field) { orderBys.add(Tuple.create(field, true)); return this; }
@Override public SmartQuery<E> orderDesc(Mapping field) { orderBys.add(Tuple.create(field, false)); return this; }
/** * Converts a {@link java.util.Map.Entry} into a tuple. * * @param entry the entry to convert * @param <K> the key type of the entry * @param <V> the value type of the entry * @return a tuple containing the key as first and the value as second parameter */ public static <K, V> Tuple<K, V> valueOf(Map.Entry<K, V> entry) { return Tuple.create(entry.getKey(), entry.getValue()); } }
@Nullable private Tuple<Integer, Boolean> getNextRelevantIndex(String sql) { int index = sql.indexOf("${"); int sharpIndex = sql.indexOf("#{"); if ((sharpIndex > -1) && ((index < 0) || (sharpIndex < index))) { return Tuple.create(sharpIndex, false); } if (index > -1) { return Tuple.create(index, true); } else { return null; } }
/** * Stores a value for the given key. * <p> * Can be used to add computed values for further processing. * * @param key the key to bind the value to * @param value the value to be stored */ public void setValue(@Nonnull String key, Object value) { fields.put(key.toUpperCase(), Tuple.create(key, value)); }
/** * Maps an entry which contains a collection as value into a {@link java.util.stream.Stream} of tuples, containing * the key of the entry along with a value of the collection. * <p> * This method is designed to be used with {@link Stream#flatMap(java.util.function.Function)}: * <pre> * {@code * map.entrySet().flatMap(e -> Tuple.flatten(e)).forEach(t -> System.out.println(t)); * } * </pre> * * @param entry the entry to transform * @param <K> the key type of the entry * @param <V> the value type of the entry * @return a <tt>Stream</tt> of tuples representing the original entry */ public static <K, V> Stream<Tuple<K, V>> flatten(Map.Entry<K, Collection<V>> entry) { return entry.getValue().stream().map(v -> Tuple.create(entry.getKey(), v)); }
/** * Returns the current mapped diagnostic context (MDC). * * @return a list of name-value pair representing the current mdc. */ public List<Tuple<String, String>> getMDC() { List<Tuple<String, String>> result = new ArrayList<>(); for (Map.Entry<String, Object> entry : mdc.entrySet()) { if (entry.getValue() instanceof Supplier) { result.add(Tuple.create(entry.getKey(), Value.of(((Supplier<?>) entry.getValue()).get()).asString())); } else { result.add(Tuple.create(entry.getKey(), String.valueOf(entry.getValue()))); } } return result; }
/** * Returns all generated keys wrapped as row * * @param stmt the statement which was used to perform an update or insert * @return a row containing all generated keys * @throws SQLException in case of an error thrown by the database or driver */ public Row fetchGeneratedKeys(PreparedStatement stmt) throws SQLException { try (ResultSet rs = stmt.getGeneratedKeys()) { Row row = new Row(); if (rs != null && rs.next()) { for (int col = 1; col <= rs.getMetaData().getColumnCount(); col++) { row.fields.put(rs.getMetaData().getColumnLabel(col).toUpperCase(), Tuple.create(rs.getMetaData().getColumnLabel(col), rs.getObject(col))); } } return row; } } }
private static XPathExpression compile(String xpath) throws XPathExpressionException { Tuple<Thread, String> key = Tuple.create(Thread.currentThread(), xpath); if (cache == null) { cache = CacheManager.createLocalCache("xpath"); } XPathExpression result = cache.get(key); if (result == null) { result = XPATH.newXPath().compile(xpath); cache.put(key, result); } return result; }
@Override protected Tuple<Double, Double> copyValue(Tuple<Double, Double> value) { return Tuple.create(value.getFirst(), value.getSecond()); }
@SuppressWarnings("unchecked") @Override protected Object transformFromDatasource(Class<? extends BaseMapper<?, ?, ?>> mapperType, Value object) { if (mapperType != Mango.class) { throw new UnsupportedOperationException( "MultiPointLocationProperty currently only supports Mango as mapper!"); } List<Tuple<Double, Double>> locations = new ArrayList<>(); if (!object.isNull()) { Object coordinates = ((Document) object.get()).get("coordinates"); if (coordinates instanceof List<?>) { ((List<List<Double>>) coordinates).stream() .map(entry -> Tuple.create(entry.get(0), entry.get(1))) .collect(Lambdas.into(locations)); } } return locations; }
/** * Generates a new property for the given field * * @param field the underlying field from which the property was created */ private CompleterProperty(Field field) { super(field); if (field.getAnnotation(FastCompletion.class).contextNames().length != field.getAnnotation(FastCompletion.class) .contextTypes().length) { throw new IllegalStateException("Different dimensions for context names and context types!"); } for (int i = 0; i < field.getAnnotation(FastCompletion.class).contextNames().length; i++) { contexts.add(Tuple.create(field.getAnnotation(FastCompletion.class).contextNames()[i], field.getAnnotation(FastCompletion.class).contextTypes()[i])); } analyzer = field.isAnnotationPresent(Analyzed.class) ? field.getAnnotation(Analyzed.class).analyzer() : Analyzed.ANALYZER_WHITESPACE; maxInputLength = field.getAnnotation(FastCompletion.class).maxInputLength(); }
private Collection<Transformer<?, ?>> getFactories(Class<?> sourceType, Class<?> targetType) { if (factories == null) { MultiMap<Tuple<Class<?>, Class<?>>, Transformer<?, ?>> result = MultiMap.createOrdered(); for (Transformer<?, ?> factory : factoryList) { result.put(Tuple.create(factory.getSourceClass(), factory.getTargetClass()), factory); } factories = result; } return factories.get(Tuple.create(sourceType, targetType)); }
/** * Splits the given string at the last occurrence of the separator. * <p> * If the given input is empty, a tuple with <tt>null</tt> as first and second component will be returned. * * @param input the input to be split * @param separator the separator used to split at * @return a <tt>Tuple</tt> containing the part before the separator as first * and the part after the separator as second component */ public static Tuple<String, String> splitAtLast(String input, String separator) { Tuple<String, String> result = Tuple.create(); if (isFilled(input)) { int idx = input.lastIndexOf(separator); if (idx > -1) { result.setFirst(input.substring(0, idx)); result.setSecond(input.substring(idx + separator.length())); } else { result.setFirst(input); } } return result; }
@Override public Object transformValue(Value value) { if (value.isEmptyString()) { return Collections.emptyList(); } List<Tuple<Double, Double>> result = new ArrayList<>(); for (String location : value.asString().split("\n")) { Tuple<String, String> latLong = Strings.split(location, ","); try { Tuple<Double, Double> coordinate = Tuple.create(Double.parseDouble(latLong.getFirst()), Double.parseDouble(latLong.getSecond())); result.add(coordinate); } catch (NumberFormatException e) { Exceptions.ignore(e); } } return result; }
@Override @SuppressWarnings("unchecked") public void onRemoval(RemovalNotification<Object, Object> notification) { if (removeListener != null) { try { CacheEntry<K, V> entry = (CacheEntry<K, V>) notification.getValue(); removeListener.invoke(Tuple.create(entry.getKey(), entry.getValue())); } catch (Exception e) { Exceptions.handle(e); } } } }
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); }
protected Tuple<String, String> parseFieldAndValue(LookaheadReader reader) { String field = defaultField; boolean couldBeFieldNameSoFar = true; StringBuilder valueBuilder = new StringBuilder(); while (!reader.current().isEndOfInput() && !reader.current().isWhitepace() && !reader.current().is(')')) { if (reader.current().is(':') && valueBuilder.length() > 0 && couldBeFieldNameSoFar) { field = valueBuilder.toString(); valueBuilder = new StringBuilder(); // Ignore : reader.consume(); couldBeFieldNameSoFar = false; } else { if (couldBeFieldNameSoFar) { couldBeFieldNameSoFar = reader.current().is('-', '_') || reader.current().isLetter() || reader.current().isDigit(); } valueBuilder.append(reader.consume().getValue()); } } return Tuple.create(field, valueBuilder.toString()); }
protected void traceIfRequired(String collection, Watch w) { if (mongo.tracing && w.elapsedMillis() >= mongo.traceLimit) { String location = determineLocation(); Doc explanation = explain(collection); mongo.traceData.put(location, Tuple.create(collection + ": " + filterObject.toString() + " [" + w.duration() + "]", explanation.toString())); } if (w.elapsedMillis() > mongo.getLogQueryThresholdMillis()) { mongo.numSlowQueries.inc(); DB.SLOW_DB_LOG.INFO("A slow MongoDB query was executed (%s): %s\n%s\n%s", w.duration(), collection, filterObject, ExecutionPoint.snapshot().toString()); } }