Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
* The double is created by parsing the string config value and converting between the time unit declared in the
* string config value and the time unit of the enclosing Config class instance. The value returned will also include
* the non-integer fraction of the time, if any, that results from converting between time units.
* See {@link StrictValueParser#asTime()} for a variant that only returns a whole number of time units.
*
* String config values representing times can be given either:
*
*
in the form "{@code ,
* Valid time units are those defined by {@link TimeUnit}
*
* @throws NullPointerException if {@link Config#getTimeUnit()} has not been set.
* @throws IllegalStateException if the config value does not satisfy one of the formats given above.
* @throws IllegalArgumentException if the time unit in the config value does not match one of the values specified by {@link TimeUnit}.
* @throws NumberFormatException if the numerical part of the config value given cannot be parsed by {@link Double#parseDouble(String)}.
*/
public double asFractionalTime() {
try {
return ConfigParsers.parseFractionalTime(value, getTimeUnit());
} catch (Throwable t) {
throw new IllegalStateException("Error parsing " + ConfigKeyUtils.getKeyName(key), t);
}
}
/**
* @return a long representing a time in the unit returned by {@link Config#getTimeUnit()}.
*
* The long is created by parsing the string config value and converting between the time unit declared in the
* string config value and the time unit of the enclosing Config class instance. The value returned will be rounded to the nearest
* whole number of units.
*
* String config values representing times can be given either:
*
*
in the form "{@code ,}" or "{@code :}". E.g. "5.65,SECONDS" or "1:HOURS"
*
as a double, in which case it will be assumed that the value is being specified with a time unit of seconds. I.e. this is equivalent to doing "{@code ,SECONDS}"
*
* Valid time units are those defined by {@link TimeUnit}
*
* @throws NullPointerException if {@link Config#getTimeUnit()} has not been set.
* @throws IllegalStateException if the config value does not satisfy one of the formats given above.
* @throws IllegalArgumentException if the time unit in the config value does not match one of the values specified by {@link TimeUnit}.
* @throws NumberFormatException if the numerical part of the config value given cannot be parsed by {@link Double#parseDouble(String)}.
*/
public long asTime() {
return Math.round(asFractionalTime());
}
/**
* @return the string config value parsed as a {@link Duration} rounded to the nearest nanosecond.
*
* Duration config values can be given either:
* - As a double on its own, in which case it will be assumed that the value is being specified in seconds
* - In the form {@code ,} or {@code :}.
*
* @throws IllegalStateException if the config value does not satisfy one of the formats given above.
* @throws IllegalArgumentException if the time unit in the config value does not match an enum value.
* @throws NumberFormatException if the value given cannot be parsed as a double.
*/
public Duration asDuration() {
try {
return ConfigParsers.parseDuration(value);
} catch (Throwable t) {
throw new IllegalStateException("Error parsing " + ConfigKeyUtils.getKeyName(key), t);
}
}
/**
* @return the string config value parsed as a length using the declared application length unit.
*
* Length config values can be given either
* - as a double, in which case Config will assume that the value is being specified in meters.
* - in the form {@code ,} or {@code :}.
*
* @throws NullPointerException if the application length unit has not been set.
* @throws IllegalStateException if the config value does not satisfy one of the formats given above.
* @throws IllegalArgumentException if the length unit in the config value does not match an enum value.
* @throws NumberFormatException if the value given cannot be parsed as a double.
*/
public double asLength() {
try {
return ConfigParsers.parseLength(value, getLengthUnit());
} catch (Throwable t) {
throw new IllegalStateException("Error parsing " + ConfigKeyUtils.getKeyName(key), t);
}
}
/**
* @return the string config value parsed as a speed using the declared application time and length units.
*
* Speed config values can be given either
* - as a double, in which case Config will assume that the value is being specified in meters per second
* - in the form {@code ,,} or {@code ::}
*
* @throws NullPointerException if the application time or length units have not been set
* @throws IllegalStateException if the config value does not satisfy one of the formats given above
* @throws IllegalArgumentException if the time or length units in the config value do not match an enum value
* @throws NumberFormatException if the value given cannot be parsed as a double
*/
public double asSpeed() {
try {
return ConfigParsers.parseSpeed(value, getLengthUnit(), getTimeUnit());
} catch (Throwable t) {
throw new IllegalStateException("Error parsing " + ConfigKeyUtils.getKeyName(key), t);
}
}
/**
* @return the string config value parsed as an acceleration using the declared application time and length units.
*
* Acceleration config values can be given either
* - as a double, in which case Config will assume that the value is being specified in meters per second squared
* - in the form {@code ,,} or {@code ::}
*
* @throws NullPointerException if the application time or length units have not been set
* @throws IllegalStateException if the config value does not satisfy one of the formats given above
* @throws IllegalArgumentException if the time or length units in the config value do not match an enum value
* @throws NumberFormatException if the value given cannot be parsed as a double
*/
public double asAcceleration() {
try {
return ConfigParsers.parseAcceleration(value, getLengthUnit(), getTimeUnit());
} catch (Throwable t) {
throw new IllegalStateException("Error parsing " + ConfigKeyUtils.getKeyName(key), t);
}
}
/**
* @return the string config value parsed as a jerk using the declared application time and length units.
*
* Jerk config values can be given either
* - as a double, in which case Config will assume that the value is being specified in meters per second cubed
* - in the form {@code ,,} or {@code ::}
*
* @throws NullPointerException if the application time or length units have not been set
* @throws IllegalStateException if the config value does not satisfy one of the formats given above
* @throws IllegalArgumentException if the time or length units in the config value do not match an enum value
* @throws NumberFormatException if the value given cannot be parsed as a double
*/
public double asJerk() {
try {
return ConfigParsers.parseJerk(value, getLengthUnit(), getTimeUnit());
} catch (Throwable t) {
throw new IllegalStateException("Error parsing " + ConfigKeyUtils.getKeyName(key), t);
}
}
/**
* @return a {@link ListValueParser} operating on the String config value.
*/
public ListValueParser asList() {
return new ListValueParser(key, value);
}
/**
* @return a {@link SetValueParser} operating on the String config value.
*/
public SetValueParser asSet() {
return new SetValueParser(key, value);
}
/**
* @return a {@link MapValueParser} operating on the String config value.
*/
public MapValueParser asMap() {
return new MapValueParser(key, value);
}
/**
* @return a {@link MapValueParser} operating on the String config value.
*/
public SetMultimapValueParser asSetMultimap() {
return new SetMultimapValueParser(key, value);
}
/**
* @return the String config value parsed using the provided custom parser.
*/
public T withCustomParser(Function parser) {
try {
return parser.apply(value);
} catch (Throwable t) {
throw new IllegalStateException("Error parsing " + ConfigKeyUtils.getKeyName(key), t);
}
}
/**
* @deprecated to help avoid calling this when {@link StrictValueParser#asString()} is desired
*/
@Deprecated
@Override
public String toString() {
return MoreObjects.toStringHelper(this)
.add("value", value)
.toString();
}
private TimeUnit getTimeUnit() {
return Preconditions.checkNotNull(timeUnit, "timeUnit not set. See ConfigManager.Builder.setTimeUnit.");
}
private LengthUnit getLengthUnit() {
return Preconditions.checkNotNull(lengthUnit, "lengthUnit not set. See ConfigManager.Builder.setLengthUnit.");
}
}