org.joda.convert.FromString Java Examples

The following examples show how to use org.joda.convert.FromString. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: DateTimeZone.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Gets a time zone instance for the specified time zone id.
 * <p>
 * The time zone id may be one of those returned by getAvailableIDs.
 * Short ids, as accepted by {@link java.util.TimeZone}, are not accepted.
 * All IDs must be specified in the long format.
 * The exception is UTC, which is an acceptable id.
 * <p>
 * Alternatively a locale independent, fixed offset, datetime zone can
 * be specified. The form <code>[+-]hh:mm</code> can be used.
 * 
 * @param id  the ID of the datetime zone, null means default
 * @return the DateTimeZone object for the ID
 * @throws IllegalArgumentException if the ID is not recognised
 */
@FromString
public static DateTimeZone forID(String id) {
    if (id == null) {
        return getDefault();
    }
    if (id.equals("UTC")) {
        return DateTimeZone.UTC;
    }
    DateTimeZone zone = cProvider.getZone(id);
    if (zone != null) {
        return zone;
    }
    if (id.startsWith("+") || id.startsWith("-")) {
        int offset = parseOffset(id);
        if (offset == 0L) {
            return DateTimeZone.UTC;
        } else {
            id = printOffset(offset);
            return fixedOffsetZone(id, offset);
        }
    }
    throw new IllegalArgumentException("The datetime zone id '" + id + "' is not recognised");
}
 
Example #2
Source File: Cardumen_00239_s.java    From coming with MIT License 6 votes vote down vote up
/**
 * Gets a time zone instance for the specified time zone id.
 * <p>
 * The time zone id may be one of those returned by getAvailableIDs.
 * Short ids, as accepted by {@link java.util.TimeZone}, are not accepted.
 * All IDs must be specified in the long format.
 * The exception is UTC, which is an acceptable id.
 * <p>
 * Alternatively a locale independent, fixed offset, datetime zone can
 * be specified. The form <code>[+-]hh:mm</code> can be used.
 * 
 * @param id  the ID of the datetime zone, null means default
 * @return the DateTimeZone object for the ID
 * @throws IllegalArgumentException if the ID is not recognised
 */
@FromString
public static DateTimeZone forID(String id) {
    if (id == null) {
        return getDefault();
    }
    if (id.equals("UTC")) {
        return DateTimeZone.UTC;
    }
    DateTimeZone zone = cProvider.getZone(id);
    if (zone != null) {
        return zone;
    }
    if (id.startsWith("+") || id.startsWith("-")) {
        int offset = parseOffset(id);
        if (offset == 0L) {
            return DateTimeZone.UTC;
        } else {
            id = printOffset(offset);
            return fixedOffsetZone(id, offset);
        }
    }
    throw new IllegalArgumentException("The datetime zone id '" + id + "' is not recognised");
}
 
Example #3
Source File: LongDoublePair.java    From Strata with Apache License 2.0 6 votes vote down vote up
/**
 * Parses a {@code LongDoublePair} 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 LongDoublePair 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);
  }
  long first = Long.parseLong(split.get(0));
  double second = Double.parseDouble(split.get(1));
  return new LongDoublePair(first, second);
}
 
Example #4
Source File: Cardumen_00189_s.java    From coming with MIT License 6 votes vote down vote up
/**
 * Gets a time zone instance for the specified time zone id.
 * <p>
 * The time zone id may be one of those returned by getAvailableIDs.
 * Short ids, as accepted by {@link java.util.TimeZone}, are not accepted.
 * All IDs must be specified in the long format.
 * The exception is UTC, which is an acceptable id.
 * <p>
 * Alternatively a locale independent, fixed offset, datetime zone can
 * be specified. The form <code>[+-]hh:mm</code> can be used.
 * 
 * @param id  the ID of the datetime zone, null means default
 * @return the DateTimeZone object for the ID
 * @throws IllegalArgumentException if the ID is not recognised
 */
@FromString
public static DateTimeZone forID(String id) {
    if (id == null) {
        return getDefault();
    }
    if (id.equals("UTC")) {
        return DateTimeZone.UTC;
    }
    DateTimeZone zone = cProvider.getZone(id);
    if (zone != null) {
        return zone;
    }
    if (id.startsWith("+") || id.startsWith("-")) {
        int offset = parseOffset(id);
        if (offset == 0L) {
            return DateTimeZone.UTC;
        } else {
            id = printOffset(offset);
            return fixedOffsetZone(id, offset);
        }
    }
    throw new IllegalArgumentException("The datetime zone id '" + id + "' is not recognised");
}
 
Example #5
Source File: Cardumen_0070_s.java    From coming with MIT License 6 votes vote down vote up
/**
 * Gets a time zone instance for the specified time zone id.
 * <p>
 * The time zone id may be one of those returned by getAvailableIDs.
 * Short ids, as accepted by {@link java.util.TimeZone}, are not accepted.
 * All IDs must be specified in the long format.
 * The exception is UTC, which is an acceptable id.
 * <p>
 * Alternatively a locale independent, fixed offset, datetime zone can
 * be specified. The form <code>[+-]hh:mm</code> can be used.
 * 
 * @param id  the ID of the datetime zone, null means default
 * @return the DateTimeZone object for the ID
 * @throws IllegalArgumentException if the ID is not recognised
 */
@FromString
public static DateTimeZone forID(String id) {
    if (id == null) {
        return getDefault();
    }
    if (id.equals("UTC")) {
        return DateTimeZone.UTC;
    }
    DateTimeZone zone = cProvider.getZone(id);
    if (zone != null) {
        return zone;
    }
    if (id.startsWith("+") || id.startsWith("-")) {
        int offset = parseOffset(id);
        if (offset == 0L) {
            return DateTimeZone.UTC;
        } else {
            id = printOffset(offset);
            return fixedOffsetZone(id, offset);
        }
    }
    throw new IllegalArgumentException("The datetime zone id '" + id + "' is not recognised");
}
 
Example #6
Source File: IntDoublePair.java    From Strata with Apache License 2.0 6 votes vote down vote up
/**
 * Parses an {@code IntDoublePair} 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 IntDoublePair 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);
  }
  int first = Integer.parseInt(split.get(0));
  double second = Double.parseDouble(split.get(1));
  return new IntDoublePair(first, second);
}
 
Example #7
Source File: Cardumen_0070_t.java    From coming with MIT License 6 votes vote down vote up
/**
 * Gets a time zone instance for the specified time zone id.
 * <p>
 * The time zone id may be one of those returned by getAvailableIDs.
 * Short ids, as accepted by {@link java.util.TimeZone}, are not accepted.
 * All IDs must be specified in the long format.
 * The exception is UTC, which is an acceptable id.
 * <p>
 * Alternatively a locale independent, fixed offset, datetime zone can
 * be specified. The form <code>[+-]hh:mm</code> can be used.
 * 
 * @param id  the ID of the datetime zone, null means default
 * @return the DateTimeZone object for the ID
 * @throws IllegalArgumentException if the ID is not recognised
 */
@FromString
public static DateTimeZone forID(String id) {
    if (id == null) {
        return getDefault();
    }
    if (id.equals("UTC")) {
        return DateTimeZone.UTC;
    }
    DateTimeZone zone = cProvider.getZone(id);
    if (zone != null) {
        return zone;
    }
    if (id.startsWith("+") || id.startsWith("-")) {
        int offset = parseOffset(id);
        if (offset == 0L) {
            return DateTimeZone.UTC;
        } else {
            id = printOffset(offset);
            return fixedOffsetZone(id, offset);
        }
    }
    throw new IllegalArgumentException("The datetime zone id '" + id + "' is not recognised");
}
 
Example #8
Source File: Cardumen_00137_s.java    From coming with MIT License 6 votes vote down vote up
/**
 * Gets a time zone instance for the specified time zone id.
 * <p>
 * The time zone id may be one of those returned by getAvailableIDs.
 * Short ids, as accepted by {@link java.util.TimeZone}, are not accepted.
 * All IDs must be specified in the long format.
 * The exception is UTC, which is an acceptable id.
 * <p>
 * Alternatively a locale independent, fixed offset, datetime zone can
 * be specified. The form <code>[+-]hh:mm</code> can be used.
 * 
 * @param id  the ID of the datetime zone, null means default
 * @return the DateTimeZone object for the ID
 * @throws IllegalArgumentException if the ID is not recognised
 */
@FromString
public static DateTimeZone forID(String id) {
    if (id == null) {
        return getDefault();
    }
    if (id.equals("UTC")) {
        return DateTimeZone.UTC;
    }
    DateTimeZone zone = cProvider.getZone(id);
    if (zone != null) {
        return zone;
    }
    if (id.startsWith("+") || id.startsWith("-")) {
        int offset = parseOffset(id);
        if (offset == 0L) {
            return DateTimeZone.UTC;
        } else {
            id = printOffset(offset);
            return fixedOffsetZone(id, offset);
        }
    }
    throw new IllegalArgumentException("The datetime zone id '" + id + "' is not recognised");
}
 
Example #9
Source File: Nopol2017_0089_t.java    From coming with MIT License 6 votes vote down vote up
/**
 * Gets a time zone instance for the specified time zone id.
 * <p>
 * The time zone id may be one of those returned by getAvailableIDs.
 * Short ids, as accepted by {@link java.util.TimeZone}, are not accepted.
 * All IDs must be specified in the long format.
 * The exception is UTC, which is an acceptable id.
 * <p>
 * Alternatively a locale independent, fixed offset, datetime zone can
 * be specified. The form <code>[+-]hh:mm</code> can be used.
 * 
 * @param id  the ID of the datetime zone, null means default
 * @return the DateTimeZone object for the ID
 * @throws IllegalArgumentException if the ID is not recognised
 */
@FromString
public static DateTimeZone forID(String id) {
    if (id == null) {
        return getDefault();
    }
    if (id.equals("UTC")) {
        return DateTimeZone.UTC;
    }
    DateTimeZone zone = cProvider.getZone(id);
    if (zone != null) {
        return zone;
    }
    if (id.startsWith("+") || id.startsWith("-")) {
        int offset = parseOffset(id);
        if (offset == 0L) {
            return DateTimeZone.UTC;
        } else {
            id = printOffset(offset);
            return fixedOffsetZone(id, offset);
        }
    }
    throw new IllegalArgumentException("The datetime zone id '" + id + "' is not recognised");
}
 
Example #10
Source File: Time_25_DateTimeZone_t.java    From coming with MIT License 6 votes vote down vote up
/**
 * Gets a time zone instance for the specified time zone id.
 * <p>
 * The time zone id may be one of those returned by getAvailableIDs.
 * Short ids, as accepted by {@link java.util.TimeZone}, are not accepted.
 * All IDs must be specified in the long format.
 * The exception is UTC, which is an acceptable id.
 * <p>
 * Alternatively a locale independent, fixed offset, datetime zone can
 * be specified. The form <code>[+-]hh:mm</code> can be used.
 * 
 * @param id  the ID of the datetime zone, null means default
 * @return the DateTimeZone object for the ID
 * @throws IllegalArgumentException if the ID is not recognised
 */
@FromString
public static DateTimeZone forID(String id) {
    if (id == null) {
        return getDefault();
    }
    if (id.equals("UTC")) {
        return DateTimeZone.UTC;
    }
    DateTimeZone zone = cProvider.getZone(id);
    if (zone != null) {
        return zone;
    }
    if (id.startsWith("+") || id.startsWith("-")) {
        int offset = parseOffset(id);
        if (offset == 0L) {
            return DateTimeZone.UTC;
        } else {
            id = printOffset(offset);
            return fixedOffsetZone(id, offset);
        }
    }
    throw new IllegalArgumentException("The datetime zone id '" + id + "' is not recognised");
}
 
Example #11
Source File: Time_23_DateTimeZone_t.java    From coming with MIT License 6 votes vote down vote up
/**
 * Gets a time zone instance for the specified time zone id.
 * <p>
 * The time zone id may be one of those returned by getAvailableIDs.
 * Short ids, as accepted by {@link java.util.TimeZone}, are not accepted.
 * All IDs must be specified in the long format.
 * The exception is UTC, which is an acceptable id.
 * <p>
 * Alternatively a locale independent, fixed offset, datetime zone can
 * be specified. The form <code>[+-]hh:mm</code> can be used.
 * 
 * @param id  the ID of the datetime zone, null means default
 * @return the DateTimeZone object for the ID
 * @throws IllegalArgumentException if the ID is not recognised
 */
@FromString
public static DateTimeZone forID(String id) {
    if (id == null) {
        return getDefault();
    }
    if (id.equals("UTC")) {
        return DateTimeZone.UTC;
    }
    DateTimeZone zone = cProvider.getZone(id);
    if (zone != null) {
        return zone;
    }
    if (id.startsWith("+") || id.startsWith("-")) {
        int offset = parseOffset(id);
        if (offset == 0L) {
            return DateTimeZone.UTC;
        } else {
            id = printOffset(offset);
            return fixedOffsetZone(id, offset);
        }
    }
    throw new IllegalArgumentException("The datetime zone id '" + id + "' is not recognised");
}
 
Example #12
Source File: Time_17_DateTimeZone_t.java    From coming with MIT License 6 votes vote down vote up
/**
 * Gets a time zone instance for the specified time zone id.
 * <p>
 * The time zone id may be one of those returned by getAvailableIDs.
 * Short ids, as accepted by {@link java.util.TimeZone}, are not accepted.
 * All IDs must be specified in the long format.
 * The exception is UTC, which is an acceptable id.
 * <p>
 * Alternatively a locale independent, fixed offset, datetime zone can
 * be specified. The form <code>[+-]hh:mm</code> can be used.
 * 
 * @param id  the ID of the datetime zone, null means default
 * @return the DateTimeZone object for the ID
 * @throws IllegalArgumentException if the ID is not recognised
 */
@FromString
public static DateTimeZone forID(String id) {
    if (id == null) {
        return getDefault();
    }
    if (id.equals("UTC")) {
        return DateTimeZone.UTC;
    }
    DateTimeZone zone = cProvider.getZone(id);
    if (zone != null) {
        return zone;
    }
    if (id.startsWith("+") || id.startsWith("-")) {
        int offset = parseOffset(id);
        if (offset == 0L) {
            return DateTimeZone.UTC;
        } else {
            id = printOffset(offset);
            return fixedOffsetZone(id, offset);
        }
    }
    throw new IllegalArgumentException("The datetime zone id '" + id + "' is not recognised");
}
 
Example #13
Source File: CurrencyAmount.java    From Strata with Apache License 2.0 6 votes vote down vote up
/**
 * Parses the string to produce a {@code CurrencyAmount}.
 * <p>
 * This parses the {@code toString} format of '${currency} ${amount}'.
 *
 * @param amountStr  the amount string
 * @return the currency amount
 * @throws IllegalArgumentException if the amount cannot be parsed
 */
@FromString
public static CurrencyAmount parse(String amountStr) {
  // this method has had some performance optimizations applied
  if (amountStr == null || amountStr.length() <= 4 || amountStr.charAt(3) != ' ') {
    throw new IllegalArgumentException("Unable to parse amount, invalid format: " + amountStr);
  }

  String currencyCode = amountStr.substring(0, 3);
  String doubleString = amountStr.substring(4);
  if (doubleString.indexOf(' ') != -1) {
    throw new IllegalArgumentException("Unable to parse amount, invalid format: " + amountStr);
  }

  try {
    Currency cur = Currency.parse(currencyCode);
    double amount = Double.parseDouble(doubleString);
    return new CurrencyAmount(cur, amount);
  } catch (RuntimeException ex) {
    throw new IllegalArgumentException("Unable to parse amount: " + amountStr, ex);
  }
}
 
Example #14
Source File: Time_9_DateTimeZone_t.java    From coming with MIT License 6 votes vote down vote up
/**
 * Gets a time zone instance for the specified time zone id.
 * <p>
 * The time zone id may be one of those returned by getAvailableIDs.
 * Short ids, as accepted by {@link java.util.TimeZone}, are not accepted.
 * All IDs must be specified in the long format.
 * The exception is UTC, which is an acceptable id.
 * <p>
 * Alternatively a locale independent, fixed offset, datetime zone can
 * be specified. The form <code>[+-]hh:mm</code> can be used.
 * 
 * @param id  the ID of the datetime zone, null means default
 * @return the DateTimeZone object for the ID
 * @throws IllegalArgumentException if the ID is not recognised
 */
@FromString
public static DateTimeZone forID(String id) {
    if (id == null) {
        return getDefault();
    }
    if (id.equals("UTC")) {
        return DateTimeZone.UTC;
    }
    DateTimeZone zone = cProvider.getZone(id);
    if (zone != null) {
        return zone;
    }
    if (id.startsWith("+") || id.startsWith("-")) {
        int offset = parseOffset(id);
        if (offset == 0L) {
            return DateTimeZone.UTC;
        } else {
            id = printOffset(offset);
            return fixedOffsetZone(id, offset);
        }
    }
    throw new IllegalArgumentException("The datetime zone id '" + id + "' is not recognised");
}
 
Example #15
Source File: Time_19_DateTimeZone_s.java    From coming with MIT License 6 votes vote down vote up
/**
 * Gets a time zone instance for the specified time zone id.
 * <p>
 * The time zone id may be one of those returned by getAvailableIDs.
 * Short ids, as accepted by {@link java.util.TimeZone}, are not accepted.
 * All IDs must be specified in the long format.
 * The exception is UTC, which is an acceptable id.
 * <p>
 * Alternatively a locale independent, fixed offset, datetime zone can
 * be specified. The form <code>[+-]hh:mm</code> can be used.
 * 
 * @param id  the ID of the datetime zone, null means default
 * @return the DateTimeZone object for the ID
 * @throws IllegalArgumentException if the ID is not recognised
 */
@FromString
public static DateTimeZone forID(String id) {
    if (id == null) {
        return getDefault();
    }
    if (id.equals("UTC")) {
        return DateTimeZone.UTC;
    }
    DateTimeZone zone = cProvider.getZone(id);
    if (zone != null) {
        return zone;
    }
    if (id.startsWith("+") || id.startsWith("-")) {
        int offset = parseOffset(id);
        if (offset == 0L) {
            return DateTimeZone.UTC;
        } else {
            id = printOffset(offset);
            return fixedOffsetZone(id, offset);
        }
    }
    throw new IllegalArgumentException("The datetime zone id '" + id + "' is not recognised");
}
 
Example #16
Source File: Time_19_DateTimeZone_t.java    From coming with MIT License 6 votes vote down vote up
/**
 * Gets a time zone instance for the specified time zone id.
 * <p>
 * The time zone id may be one of those returned by getAvailableIDs.
 * Short ids, as accepted by {@link java.util.TimeZone}, are not accepted.
 * All IDs must be specified in the long format.
 * The exception is UTC, which is an acceptable id.
 * <p>
 * Alternatively a locale independent, fixed offset, datetime zone can
 * be specified. The form <code>[+-]hh:mm</code> can be used.
 * 
 * @param id  the ID of the datetime zone, null means default
 * @return the DateTimeZone object for the ID
 * @throws IllegalArgumentException if the ID is not recognised
 */
@FromString
public static DateTimeZone forID(String id) {
    if (id == null) {
        return getDefault();
    }
    if (id.equals("UTC")) {
        return DateTimeZone.UTC;
    }
    DateTimeZone zone = cProvider.getZone(id);
    if (zone != null) {
        return zone;
    }
    if (id.startsWith("+") || id.startsWith("-")) {
        int offset = parseOffset(id);
        if (offset == 0L) {
            return DateTimeZone.UTC;
        } else {
            id = printOffset(offset);
            return fixedOffsetZone(id, offset);
        }
    }
    throw new IllegalArgumentException("The datetime zone id '" + id + "' is not recognised");
}
 
Example #17
Source File: Frequency.java    From Strata with Apache License 2.0 6 votes vote down vote up
/**
 * Parses a formatted string representing the frequency.
 * <p>
 * The format can either be based on ISO-8601, such as 'P3M'
 * or without the 'P' prefix e.g. '2W'.
 * <p>
 * The period must be positive and non-zero.
 *
 * @param toParse  the string representing the frequency
 * @return the frequency
 * @throws IllegalArgumentException if the frequency cannot be parsed
 */
@FromString
public static Frequency parse(String toParse) {
  ArgChecker.notNull(toParse, "toParse");
  if (toParse.equalsIgnoreCase("Term") ||
      toParse.equalsIgnoreCase("T") ||
      toParse.equalsIgnoreCase("0T") ||
      toParse.equalsIgnoreCase("1T")) {
    return TERM;
  }
  String prefixed = toParse.startsWith("P") ? toParse : "P" + toParse;
  try {
    return Frequency.of(Period.parse(prefixed));
  } catch (DateTimeParseException ex) {
    throw new IllegalArgumentException(ex);
  }
}
 
Example #18
Source File: ResourceLocator.java    From Strata with Apache License 2.0 6 votes vote down vote up
/**
 * Creates a resource from a string locator.
 * <p>
 * This accepts locators starting with 'classpath:', 'url:' or 'file:'.
 * It also accepts unprefixed locators, treated as files.
 * 
 * @param locator  the string form of the resource locator
 * @return the resource locator
 */
@FromString
public static ResourceLocator of(String locator) {
  ArgChecker.notNull(locator, "locator");
  try {
    if (locator.startsWith(CLASSPATH_URL_PREFIX)) {
      String urlStr = locator.substring(CLASSPATH_URL_PREFIX.length());
      return ofClasspath(urlStr);

    } else if (locator.startsWith(FILE_URL_PREFIX)) {
      String fileStr = locator.substring(FILE_URL_PREFIX.length());
      return ofFile(new File(fileStr));

    } else if (locator.startsWith(URL_PREFIX)) {
      String pathStr = locator.substring(URL_PREFIX.length());
      return ofUrl(new URL(pathStr));

    } else {
      return ofFile(new File(locator));
    }
  } catch (Exception ex) {
    throw new IllegalArgumentException("Invalid resource locator: " + locator, ex);
  }
}
 
Example #19
Source File: Time_23_DateTimeZone_s.java    From coming with MIT License 6 votes vote down vote up
/**
 * Gets a time zone instance for the specified time zone id.
 * <p>
 * The time zone id may be one of those returned by getAvailableIDs.
 * Short ids, as accepted by {@link java.util.TimeZone}, are not accepted.
 * All IDs must be specified in the long format.
 * The exception is UTC, which is an acceptable id.
 * <p>
 * Alternatively a locale independent, fixed offset, datetime zone can
 * be specified. The form <code>[+-]hh:mm</code> can be used.
 * 
 * @param id  the ID of the datetime zone, null means default
 * @return the DateTimeZone object for the ID
 * @throws IllegalArgumentException if the ID is not recognised
 */
@FromString
public static DateTimeZone forID(String id) {
    if (id == null) {
        return getDefault();
    }
    if (id.equals("UTC")) {
        return DateTimeZone.UTC;
    }
    DateTimeZone zone = cProvider.getZone(id);
    if (zone != null) {
        return zone;
    }
    if (id.startsWith("+") || id.startsWith("-")) {
        int offset = parseOffset(id);
        if (offset == 0L) {
            return DateTimeZone.UTC;
        } else {
            id = printOffset(offset);
            return fixedOffsetZone(id, offset);
        }
    }
    throw new IllegalArgumentException("The datetime zone id '" + id + "' is not recognised");
}
 
Example #20
Source File: CurrencyPair.java    From Strata with Apache License 2.0 5 votes vote down vote up
/**
 * Parses a currency pair from a string with format AAA/BBB.
 * <p>
 * The parsed format is '${baseCurrency}/${counterCurrency}'.
 * Currency parsing is case insensitive.
 * 
 * @param pairStr  the currency pair as a string AAA/BBB
 * @return the currency pair
 * @throws IllegalArgumentException if the pair cannot be parsed
 */
@FromString
public static CurrencyPair parse(String pairStr) {
  ArgChecker.notNull(pairStr, "pairStr");
  Matcher matcher = REGEX_FORMAT.matcher(pairStr.toUpperCase(Locale.ENGLISH));
  if (!matcher.matches()) {
    throw new IllegalArgumentException("Invalid currency pair: " + pairStr);
  }
  Currency base = Currency.parse(matcher.group(1));
  Currency counter = Currency.parse(matcher.group(2));
  return new CurrencyPair(base, counter);
}
 
Example #21
Source File: Minutes.java    From astor with GNU General Public License v2.0 3 votes vote down vote up
/**
 * Creates a new <code>Minutes</code> by parsing a string in the ISO8601 format 'PTnM'.
 * <p>
 * The parse will accept the full ISO syntax of PnYnMnWnDTnHnMnS however only the
 * minutes component may be non-zero. If any other component is non-zero, an exception
 * will be thrown.
 *
 * @param periodStr  the period string, null returns zero
 * @return the period in minutes
 * @throws IllegalArgumentException if the string format is invalid
 */
@FromString
public static Minutes parseMinutes(String periodStr) {
    if (periodStr == null) {
        return Minutes.ZERO;
    }
    Period p = PARSER.parsePeriod(periodStr);
    return Minutes.minutes(p.getMinutes());
}
 
Example #22
Source File: Weeks.java    From astor with GNU General Public License v2.0 3 votes vote down vote up
/**
 * Creates a new <code>Weeks</code> by parsing a string in the ISO8601 format 'PnW'.
 * <p>
 * The parse will accept the full ISO syntax of PnYnMnWnDTnHnMnS however only the
 * weeks component may be non-zero. If any other component is non-zero, an exception
 * will be thrown.
 *
 * @param periodStr  the period string, null returns zero
 * @return the period in weeks
 * @throws IllegalArgumentException if the string format is invalid
 */
@FromString
public static Weeks parseWeeks(String periodStr) {
    if (periodStr == null) {
        return Weeks.ZERO;
    }
    Period p = PARSER.parsePeriod(periodStr);
    return Weeks.weeks(p.getWeeks());
}
 
Example #23
Source File: StandardId.java    From Strata with Apache License 2.0 3 votes vote down vote up
/**
 * Parses an {@code StandardId} from a formatted scheme and value.
 * <p>
 * This parses the identifier from the form produced by {@code toString()}
 * which is '{@code $scheme~$value}'.
 *
 * @param str  the identifier to parse
 * @return the identifier
 * @throws IllegalArgumentException if the identifier cannot be parsed
 */
@FromString
public static StandardId parse(String str) {
  int pos = ArgChecker.notNull(str, "str").indexOf("~");
  if (pos < 0) {
    throw new IllegalArgumentException("Invalid identifier format: " + str);
  }
  return new StandardId(str.substring(0, pos), str.substring(pos + 1));
}
 
Example #24
Source File: AttributeType.java    From Strata with Apache License 2.0 3 votes vote down vote up
/**
 * Obtains an instance from the specified name, which should be pre-registered.
 * <p>
 * The name may contain any character, but must not be empty.
 *
 * @param <T>  the type associated with the info
 * @param name  the name
 * @return an instance with the specified name
 * @throws IllegalArgumentException if the instance is not registered
 */
@SuppressWarnings("unchecked")
@FromString
public static <T> AttributeType<T> of(String name) {
  ArgChecker.notEmpty(name, "name");
  return (AttributeType<T>) INSTANCES.computeIfAbsent(name, key -> new AttributeType<T>(key));
}
 
Example #25
Source File: Weeks.java    From astor with GNU General Public License v2.0 3 votes vote down vote up
/**
 * Creates a new <code>Weeks</code> by parsing a string in the ISO8601 format 'PnW'.
 * <p>
 * The parse will accept the full ISO syntax of PnYnMnWnDTnHnMnS however only the
 * weeks component may be non-zero. If any other component is non-zero, an exception
 * will be thrown.
 *
 * @param periodStr  the period string, null returns zero
 * @return the period in weeks
 * @throws IllegalArgumentException if the string format is invalid
 */
@FromString
public static Weeks parseWeeks(String periodStr) {
    if (periodStr == null) {
        return Weeks.ZERO;
    }
    Period p = PARSER.parsePeriod(periodStr);
    return Weeks.weeks(p.getWeeks());
}
 
Example #26
Source File: Months.java    From astor with GNU General Public License v2.0 3 votes vote down vote up
/**
 * Creates a new <code>Months</code> by parsing a string in the ISO8601 format 'PnM'.
 * <p>
 * The parse will accept the full ISO syntax of PnYnMnWnDTnHnMnS however only the
 * months component may be non-zero. If any other component is non-zero, an exception
 * will be thrown.
 *
 * @param periodStr  the period string, null returns zero
 * @return the period in months
 * @throws IllegalArgumentException if the string format is invalid
 */
@FromString
public static Months parseMonths(String periodStr) {
    if (periodStr == null) {
        return Months.ZERO;
    }
    Period p = PARSER.parsePeriod(periodStr);
    return Months.months(p.getMonths());
}
 
Example #27
Source File: Hours.java    From astor with GNU General Public License v2.0 3 votes vote down vote up
/**
 * Creates a new <code>Hours</code> by parsing a string in the ISO8601 format 'PTnH'.
 * <p>
 * The parse will accept the full ISO syntax of PnYnMnWnDTnHnMnS however only the
 * hours component may be non-zero. If any other component is non-zero, an exception
 * will be thrown.
 *
 * @param periodStr  the period string, null returns zero
 * @return the period in hours
 * @throws IllegalArgumentException if the string format is invalid
 */
@FromString
public static Hours parseHours(String periodStr) {
    if (periodStr == null) {
        return Hours.ZERO;
    }
    Period p = PARSER.parsePeriod(periodStr);
    return Hours.hours(p.getHours());
}
 
Example #28
Source File: Days.java    From astor with GNU General Public License v2.0 3 votes vote down vote up
/**
 * Creates a new <code>Days</code> by parsing a string in the ISO8601 format 'PnD'.
 * <p>
 * The parse will accept the full ISO syntax of PnYnMnWnDTnHnMnS however only the
 * days component may be non-zero. If any other component is non-zero, an exception
 * will be thrown.
 *
 * @param periodStr  the period string, null returns zero
 * @return the period in days
 * @throws IllegalArgumentException if the string format is invalid
 */
@FromString
public static Days parseDays(String periodStr) {
    if (periodStr == null) {
        return Days.ZERO;
    }
    Period p = PARSER.parsePeriod(periodStr);
    return Days.days(p.getDays());
}
 
Example #29
Source File: Hours.java    From astor with GNU General Public License v2.0 3 votes vote down vote up
/**
 * Creates a new <code>Hours</code> by parsing a string in the ISO8601 format 'PTnH'.
 * <p>
 * The parse will accept the full ISO syntax of PnYnMnWnDTnHnMnS however only the
 * hours component may be non-zero. If any other component is non-zero, an exception
 * will be thrown.
 *
 * @param periodStr  the period string, null returns zero
 * @return the period in hours
 * @throws IllegalArgumentException if the string format is invalid
 */
@FromString
public static Hours parseHours(String periodStr) {
    if (periodStr == null) {
        return Hours.ZERO;
    }
    Period p = PARSER.parsePeriod(periodStr);
    return Hours.hours(p.getHours());
}
 
Example #30
Source File: Years.java    From astor with GNU General Public License v2.0 3 votes vote down vote up
/**
 * Creates a new <code>Years</code> by parsing a string in the ISO8601 format 'PnY'.
 * <p>
 * The parse will accept the full ISO syntax of PnYnMnWnDTnHnMnS however only the
 * years component may be non-zero. If any other component is non-zero, an exception
 * will be thrown.
 *
 * @param periodStr  the period string, null returns zero
 * @return the period in years
 * @throws IllegalArgumentException if the string format is invalid
 */
@FromString
public static Years parseYears(String periodStr) {
    if (periodStr == null) {
        return Years.ZERO;
    }
    Period p = PARSER.parsePeriod(periodStr);
    return Years.years(p.getYears());
}