/** * Obtains an instance from two {@code double} elements. * * @param first the first element * @param second the second element * @return a pair formed from the two parameters */ public static DoublesPair of(double first, double second) { return new DoublesPair(first, second); }
@Override public DoublesPair build() { return new DoublesPair( first, second); }
/** * Parses a {@code DoublesPair} from the standard string format. * <p> * The standard format is '[$first, $second]'. Spaces around the values are trimmed. * * @param pairStr the text to parse * @return the parsed pair * @throws IllegalArgumentException if the pair cannot be parsed */ @FromString public static DoublesPair parse(String pairStr) { ArgChecker.notNull(pairStr, "pairStr"); if (pairStr.length() < 5) { throw new IllegalArgumentException("Invalid pair format, too short: " + pairStr); } if (pairStr.charAt(0) != '[') { throw new IllegalArgumentException("Invalid pair format, must start with [: " + pairStr); } if (pairStr.charAt(pairStr.length() - 1) != ']') { throw new IllegalArgumentException("Invalid pair format, must end with ]: " + pairStr); } String content = pairStr.substring(1, pairStr.length() - 1); List<String> split = Splitter.on(',').trimResults().splitToList(content); if (split.size() != 2) { throw new IllegalArgumentException("Invalid pair format, must have two values: " + pairStr); } double first = Double.parseDouble(split.get(0)); double second = Double.parseDouble(split.get(1)); return new DoublesPair(first, second); }
/** * Obtains an instance from a {@code Pair}. * * @param pair the pair to convert * @return a pair formed by extracting values from the pair */ public static DoublesPair ofPair(Pair<Double, Double> pair) { ArgChecker.notNull(pair, "pair"); return new DoublesPair(pair.getFirst(), pair.getSecond()); }