mv com/google/domain/registry google/registry

This change renames directories in preparation for the great package
rename. The repository is now in a broken state because the code
itself hasn't been updated. However this should ensure that git
correctly preserves history for each file.
This commit is contained in:
Justine Tunney 2016-05-13 18:55:08 -04:00
parent a41677aea1
commit 5012893c1d
2396 changed files with 0 additions and 0 deletions

View file

@ -0,0 +1,23 @@
package(
default_visibility = ["//java/com/google/domain/registry:registry_project"],
)
java_library(
name = "params",
visibility = ["//visibility:public"],
srcs = glob(["*.java"]),
deps = [
"//java/com/google/common/annotations",
"//java/com/google/common/base",
"//java/com/google/common/collect",
"//java/com/google/common/net",
"//java/com/google/common/primitives",
"//java/com/google/domain/registry/model",
"//java/com/google/domain/registry/util",
"//third_party/java/jcommander",
"//third_party/java/joda_money",
"//third_party/java/joda_time",
"//third_party/java/jsr305_annotations",
],
)

View file

@ -0,0 +1,58 @@
// Copyright 2016 The Domain Registry Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package com.google.domain.registry.tools.params;
import static org.joda.time.DateTimeZone.UTC;
import com.google.common.primitives.Longs;
import org.joda.time.DateTime;
import org.joda.time.format.DateTimeFormatter;
import org.joda.time.format.DateTimeFormatterBuilder;
import org.joda.time.format.DateTimeParser;
import org.joda.time.format.ISODateTimeFormat;
/** {@linkplain DateTime} CLI parameter converter/validator. Can be ISO or millis from epoch. */
public final class DateTimeParameter extends ParameterConverterValidator<DateTime> {
public DateTimeParameter() {
super("not an ISO-8601 timestamp (or millis from epoch)");
}
/**
* Parser for DateTimes that permits only a restricted subset of ISO 8601 datetime syntax.
* The supported format is "YYYY-MM-DD'T'HH:MM:SS[.SSS]ZZ", i.e. there must be a complete date
* and at least hours, minutes, seconds, and time zone; milliseconds are optional.
* <p>
* We use this instead of the default {@link ISODateTimeFormat#dateTimeParser()} because that
* parser is very flexible and accepts date times with missing dates, missing dates, and various
* other unspecified fields that can lead to confusion and ambiguity.
*/
private static final DateTimeFormatter STRICT_DATE_TIME_PARSER = new DateTimeFormatterBuilder()
.append(null, new DateTimeParser[] {
// The formatter will try the following parsers in order until one succeeds.
ISODateTimeFormat.dateTime().getParser(),
ISODateTimeFormat.dateTimeNoMillis().getParser()})
.toFormatter();
@Override
public DateTime convert(String value) {
Long millis = Longs.tryParse(value);
if (millis != null) {
return new DateTime(millis.longValue(), UTC);
}
return DateTime.parse(value, STRICT_DATE_TIME_PARSER).withZone(UTC);
}
}

View file

@ -0,0 +1,31 @@
// Copyright 2016 The Domain Registry Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package com.google.domain.registry.tools.params;
import org.joda.time.Duration;
import org.joda.time.Period;
/** Duration CLI parameter converter/validator. */
public final class DurationParameter extends ParameterConverterValidator<Duration> {
public DurationParameter() {
super("not an ISO-8601 duration (e.g. PT36H)");
}
@Override
public Duration convert(String value) {
return Period.parse(value).toStandardDuration();
}
}

View file

@ -0,0 +1,37 @@
// Copyright 2016 The Domain Registry Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package com.google.domain.registry.tools.params;
import com.google.domain.registry.model.EppResource;
import com.google.domain.registry.model.contact.ContactResource;
import com.google.domain.registry.model.domain.DomainBase;
import com.google.domain.registry.model.host.HostResource;
/** Enum to make it easy for a command to accept a flag that specifies an EppResource subclass. */
public enum EppResourceTypeParameter {
CONTACT(ContactResource.class),
DOMAIN(DomainBase.class),
HOST(HostResource.class);
private final Class<? extends EppResource> type;
private EppResourceTypeParameter(Class<? extends EppResource> type) {
this.type = type;
}
public Class<? extends EppResource> getType() {
return type;
}
}

View file

@ -0,0 +1,26 @@
// Copyright 2016 The Domain Registry Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package com.google.domain.registry.tools.params;
import com.google.common.net.HostAndPort;
/** {@linkplain HostAndPort} CLI parameter converter/validator. */
public final class HostAndPortParameter extends ParameterConverterValidator<HostAndPort> {
@Override
public HostAndPort convert(String value) {
return HostAndPort.fromString(value);
}
}

View file

@ -0,0 +1,31 @@
// Copyright 2016 The Domain Registry Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package com.google.domain.registry.tools.params;
import com.google.common.net.InternetDomainName;
/** Duration CLI parameter converter/validator. */
public final class InternetDomainNameParameter
extends ParameterConverterValidator<InternetDomainName> {
public InternetDomainNameParameter() {
super("not a fully qualified domain name");
}
@Override
public InternetDomainName convert(String value) {
return InternetDomainName.from(value);
}
}

View file

@ -0,0 +1,31 @@
// Copyright 2016 The Domain Registry Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package com.google.domain.registry.tools.params;
import org.joda.time.LocalDate;
import org.joda.time.format.ISODateTimeFormat;
/** {@linkplain LocalDate} CLI parameter converter/validator. */
public final class LocalDateParameter extends ParameterConverterValidator<LocalDate> {
public LocalDateParameter() {
super("not a local date in YYYY-MM-DD format");
}
@Override
public LocalDate convert(String value) {
return LocalDate.parse(value, ISODateTimeFormat.date());
}
}

View file

@ -0,0 +1,35 @@
// Copyright 2016 The Domain Registry Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package com.google.domain.registry.tools.params;
import java.util.logging.Level;
/**
* Java logging level CLI parameter converter/validator.
* <p>
* If Level were an enum then this wouldn't be needed since JCommander has built-in conversion
* support for enums, but Level is actually just a regular class with some static instances.
*/
public final class LoggingLevelParameter extends ParameterConverterValidator<Level> {
public LoggingLevelParameter() {
super("not a recognized logging level");
}
@Override
public Level convert(String value) {
return Level.parse(value);
}
}

View file

@ -0,0 +1,24 @@
// Copyright 2016 The Domain Registry Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package com.google.domain.registry.tools.params;
/** Long CLI parameter converter/validator. */
public class LongParameter extends ParameterConverterValidator<Long> {
@Override
public Long convert(String value) {
return Long.valueOf(value);
}
}

View file

@ -0,0 +1,40 @@
// Copyright 2016 The Domain Registry Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package com.google.domain.registry.tools.params;
import com.google.common.base.Ascii;
import org.joda.money.Money;
/** {@linkplain Money} CLI parameter converter/validator. */
public final class MoneyParameter extends ParameterConverterValidator<Money> {
public MoneyParameter() {
super("not valid money (e.g. 'USD4.99')");
}
@Override
public Money convert(String value) {
return parse(value);
}
private static Money parse(String value) {
// Add space after currency code if it's missing, to make the CLI a bit friendlier.
if (value.length() > 3 && value.charAt(3) != ' ') {
value = String.format("%s %s", value.substring(0, 3), value.substring(3));
}
return Money.parse(Ascii.toUpperCase(value));
}
}

View file

@ -0,0 +1,19 @@
// Copyright 2016 The Domain Registry Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package com.google.domain.registry.tools.params;
/** Optional wrapper for LongParameter. */
public final class OptionalLongParameter
extends OptionalParameterConverterValidator<Long, LongParameter> {}

View file

@ -0,0 +1,47 @@
// Copyright 2016 The Domain Registry Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package com.google.domain.registry.tools.params;
import com.google.common.base.Optional;
import com.google.domain.registry.util.TypeUtils.TypeInstantiator;
/**
* Class for parameters that can handle special string "null" or empty values to
* indicate a desire to pass an empty value (i.e. when clearing out nullable
* fields on a resource).
*/
public class OptionalParameterConverterValidator<T, C extends ParameterConverterValidator<T>>
extends ParameterConverterValidator<Optional<T>> {
private static final String NULL_STRING = "null";
ParameterConverterValidator<T> validator = new TypeInstantiator<C>(getClass()){}.instantiate();
@Override
public void validate(String name, String value) {
if (!value.isEmpty() && !value.equals(NULL_STRING)) {
validator.validate(name, value);
}
}
@Override
public final Optional<T> convert(String value) {
if (value.equals(NULL_STRING) || value.isEmpty()) {
return Optional.absent();
} else {
return Optional.of(validator.convert(value));
}
}
}

View file

@ -0,0 +1,19 @@
// Copyright 2016 The Domain Registry Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package com.google.domain.registry.tools.params;
/** Optional wrapper for PhoneNumberParameter. */
public final class OptionalPhoneNumberParameter
extends OptionalParameterConverterValidator<String, PhoneNumberParameter> {}

View file

@ -0,0 +1,19 @@
// Copyright 2016 The Domain Registry Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package com.google.domain.registry.tools.params;
/** Optional wrapper for StringParameter. */
public final class OptionalStringParameter
extends OptionalParameterConverterValidator<String, StringParameter> {}

View file

@ -0,0 +1,49 @@
// Copyright 2016 The Domain Registry Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package com.google.domain.registry.tools.params;
import com.beust.jcommander.IParameterValidator;
import com.beust.jcommander.IStringConverter;
import com.beust.jcommander.ParameterException;
/** Base class for parameters that do both conversion and validation (reduces boilerplate). */
public abstract class ParameterConverterValidator<T>
implements IStringConverter<T>, IParameterValidator {
private final String messageForInvalid;
ParameterConverterValidator() {
this("Validation failed.");
}
ParameterConverterValidator(String messageForInvalid) {
this.messageForInvalid = messageForInvalid;
}
@Override
public abstract T convert(String arg0); // Redefine so non-null package annotation kicks in.
@Override
public void validate(String name, String value) throws ParameterException {
try {
convert(value);
} catch (IllegalArgumentException e) {
ParameterException pe =
new ParameterException(String.format("%s=%s %s", name, value, messageForInvalid));
pe.initCause(e);
throw pe;
}
}
}

View file

@ -0,0 +1,59 @@
// Copyright 2016 The Domain Registry Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package com.google.domain.registry.tools.params;
import com.google.common.collect.ImmutableMap;
import com.google.common.net.HostAndPort;
import com.google.common.net.InternetDomainName;
import com.beust.jcommander.IStringConverter;
import com.beust.jcommander.IStringConverterFactory;
import org.joda.money.Money;
import org.joda.time.DateTime;
import org.joda.time.Duration;
import org.joda.time.LocalDate;
import org.joda.time.YearMonth;
import java.nio.file.Path;
import java.util.Map;
import java.util.logging.Level;
import javax.annotation.Nullable;
/** JCommander converter factory that works for non-internal converters. */
public final class ParameterFactory implements IStringConverterFactory {
/** Returns JCommander converter for a given type, or {@code null} if none exists. */
@Nullable
@Override
@SuppressWarnings("unchecked")
public <T> Class<? extends IStringConverter<T>> getConverter(@Nullable Class<T> type) {
return (Class<? extends IStringConverter<T>>) CONVERTERS.get(type);
}
private static final Map<Class<?>, Class<? extends IStringConverter<?>>> CONVERTERS =
new ImmutableMap.Builder<Class<?>, Class<? extends IStringConverter<?>>>()
.put(DateTime.class, DateTimeParameter.class)
.put(Duration.class, DurationParameter.class)
.put(HostAndPort.class, HostAndPortParameter.class)
.put(InternetDomainName.class, InternetDomainNameParameter.class)
.put(Level.class, LoggingLevelParameter.class)
.put(LocalDate.class, LocalDateParameter.class)
.put(Money.class, MoneyParameter.class)
.put(Path.class, PathParameter.class)
.put(YearMonth.class, YearMonthParameter.class)
.build();
}

View file

@ -0,0 +1,101 @@
// Copyright 2016 The Domain Registry Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package com.google.domain.registry.tools.params;
import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Preconditions.checkNotNull;
import com.beust.jcommander.ParameterException;
import java.net.URI;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.regex.Pattern;
/** Filesystem path CLI parameter converter/validator. */
public class PathParameter extends ParameterConverterValidator<Path> {
public PathParameter() {
super("not a valid path");
}
/** Heuristic for determining if a path value appears to be a {@link URI}. */
private static final Pattern URI_PATTERN = Pattern.compile("[a-z][-+.a-z]+:[^:]");
@Override
public final Path convert(String value) {
checkArgument(!checkNotNull(value).isEmpty());
if (URI_PATTERN.matcher(value).lookingAt()) {
return Paths.get(URI.create(value)).normalize();
}
return Paths.get(value).normalize();
}
/** {@linkplain PathParameter} when you want an output file parameter. */
public static final class OutputFile extends PathParameter {
@Override
public void validate(String name, String value) throws ParameterException {
super.validate(name, value);
Path file = convert(value).toAbsolutePath();
if (Files.exists(file)) {
if (Files.isDirectory(file)) {
throw new ParameterException(String.format("%s is a directory: %s", name, file));
}
if (!Files.isWritable(file)) {
throw new ParameterException(String.format("%s not writable: %s", name, file));
}
} else {
Path dir = file.getParent();
if (!Files.exists(dir)) {
throw new ParameterException(String.format("%s parent dir doesn't exist: %s", name, dir));
}
if (!Files.isDirectory(dir)) {
throw new ParameterException(String.format("%s parent is non-directory: %s", name, dir));
}
}
}
}
/** {@linkplain PathParameter} when you want an output directory parameter. */
public static final class OutputDirectory extends PathParameter {
@Override
public void validate(String name, String value) throws ParameterException {
super.validate(name, value);
Path file = convert(value).toAbsolutePath();
if (!Files.isDirectory(file)) {
throw new ParameterException(String.format("%s not a directory: %s", name, file));
}
}
}
/** {@linkplain PathParameter} when you want an input file that must exist. */
public static final class InputFile extends PathParameter {
@Override
public void validate(String name, String value) throws ParameterException {
super.validate(name, value);
Path file = convert(value).toAbsolutePath();
if (!Files.exists(file)) {
throw new ParameterException(String.format("%s not found: %s", name, file));
}
if (!Files.isReadable(file)) {
throw new ParameterException(String.format("%s not readable: %s", name, file));
}
if (Files.isDirectory(file)) {
throw new ParameterException(String.format("%s is a directory: %s", name, file));
}
}
}
}

View file

@ -0,0 +1,35 @@
// Copyright 2016 The Domain Registry Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package com.google.domain.registry.tools.params;
import static com.google.common.base.Preconditions.checkArgument;
import java.util.regex.Pattern;
/** Phone number CLI parameter converter/validator. */
public final class PhoneNumberParameter extends ParameterConverterValidator<String> {
private static final Pattern ICANN_E164 = Pattern.compile("(\\+[0-9]{1,3}\\.[0-9]{1,14})?");
public PhoneNumberParameter() {
super("Must be a valid +E.164 phone number, e.g. +1.2125650000");
}
@Override
public String convert(String value) {
checkArgument(ICANN_E164.matcher(value).matches());
return value;
}
}

View file

@ -0,0 +1,24 @@
// Copyright 2016 The Domain Registry Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package com.google.domain.registry.tools.params;
/** Identity String CLI parameter converter/validator. */
public class StringParameter extends ParameterConverterValidator<String> {
@Override
public String convert(String value) {
return value;
}
}

View file

@ -0,0 +1,89 @@
// Copyright 2016 The Domain Registry Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package com.google.domain.registry.tools.params;
import static com.google.common.base.Preconditions.checkArgument;
import com.google.common.base.Splitter;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSortedMap;
import com.google.common.collect.Ordering;
import com.google.domain.registry.model.registry.Registry.TldState;
import org.joda.money.Money;
import org.joda.time.DateTime;
import java.util.Map;
/**
* Combined converter and validator class for transition list JCommander argument strings.
* <p>
* These strings have the form {@code <DateTime>=<T-str>,[<DateTime>=<T-str>]*} where
* {@code <T-str>} is a string that can be parsed into an instance of some value type {@code T},
* and the entire argument represents a series of timed transitions of some property taking on
* those values. This class converts such a string into an ImmutableSortedMap mapping DateTime to
* {@code T}. Validation and conversion share the same logic; validation is just done by attempting
* conversion and throwing exceptions if need be.
* <p>
* Subclasses must implement parseValue() to define how to parse {@code <T-str>} into a {@code T}.
*
* @param <T> instance value type
*/
// TODO(b/19031334): Investigate making this complex generic type work with the factory.
public abstract class TransitionListParameter<T>
extends ParameterConverterValidator<ImmutableSortedMap<DateTime, T>> {
private static final DateTimeParameter DATE_TIME_CONVERTER = new DateTimeParameter();
public TransitionListParameter() {
super("Not formatted correctly or has transition times out of order.");
}
/** Override to define how to parse rawValue into an object of type T. */
protected abstract T parseValue(String rawValue);
@Override
public ImmutableSortedMap<DateTime, T> convert(String transitionListString) {
ImmutableMap.Builder<DateTime, T> builder = new ImmutableMap.Builder<>();
for (Map.Entry<String, String> entry :
Splitter.on(',').withKeyValueSeparator('=').split(transitionListString).entrySet()) {
builder.put(
DATE_TIME_CONVERTER.convert(entry.getKey()),
parseValue(entry.getValue()));
}
ImmutableMap<DateTime, T> transitionMap = builder.build();
checkArgument(Ordering.natural().isOrdered(transitionMap.keySet()),
"Transition times out of order.");
return ImmutableSortedMap.copyOf(transitionMap);
}
/** Converter-validator for TLD state transitions. */
public static class TldStateTransitions extends TransitionListParameter<TldState> {
@Override
protected TldState parseValue(String value) {
return TldState.valueOf(value);
}
}
/** Converter-validator for billing cost transitions. */
public static class BillingCostTransitions extends TransitionListParameter<Money> {
private static final MoneyParameter MONEY_CONVERTER = new MoneyParameter();
@Override
protected Money parseValue(String value) {
return MONEY_CONVERTER.convert(value);
}
}
}

View file

@ -0,0 +1,31 @@
// Copyright 2016 The Domain Registry Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package com.google.domain.registry.tools.params;
import org.joda.time.YearMonth;
import org.joda.time.format.ISODateTimeFormat;
/** {@linkplain YearMonth} CLI parameter converter/validator (e.g. 1984-12) */
public final class YearMonthParameter extends ParameterConverterValidator<YearMonth> {
public YearMonthParameter() {
super("not a valid YearMonth (YYYY-MM)");
}
@Override
public YearMonth convert(String value) {
return YearMonth.parse(value, ISODateTimeFormat.yearMonth());
}
}

View file

@ -0,0 +1,16 @@
// Copyright 2016 The Domain Registry Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
@javax.annotation.ParametersAreNonnullByDefault
package com.google.domain.registry.tools.params;