com.aoindustries.aoserv.client.validator.DomainLabels Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of aoserv-client Show documentation
Show all versions of aoserv-client Show documentation
Java client for the AOServ Platform.
/*
* aoserv-client - Java client for the AOServ platform.
* Copyright (C) 2011-2013, 2016 AO Industries, Inc.
* [email protected]
* 7262 Bull Pen Cir
* Mobile, AL 36695
*
* This file is part of aoserv-client.
*
* aoserv-client is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* aoserv-client is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with aoserv-client. If not, see .
*/
package com.aoindustries.aoserv.client.validator;
import com.aoindustries.aoserv.client.DtoFactory;
import com.aoindustries.io.FastExternalizable;
import com.aoindustries.io.FastObjectInput;
import com.aoindustries.io.FastObjectOutput;
import com.aoindustries.util.Internable;
import java.io.IOException;
import java.io.InvalidObjectException;
import java.io.ObjectInput;
import java.io.ObjectInputValidation;
import java.io.ObjectOutput;
import java.util.Locale;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
/**
* Represents a set of one or more domain labels. These may be prepended to a domain name
* and, as long as the total length is not exceeded, will result in a valid domain name.
*
* - Be non-null
* - Be non-empty
* - May not exceed DomainName.MAX_LENGTH
* - Have at least one domain label, each label separated by dots.
*
*
* @author AO Industries, Inc.
*/
final public class DomainLabels
implements
Comparable,
FastExternalizable,
ObjectInputValidation,
DtoFactory,
Internable
{
/**
* Validates a set of domain labels.
*
* @see DomainLabel#validate(java.lang.String)
*/
public static ValidationResult validate(String labels) {
if(labels==null) return new InvalidResult(ApplicationResources.accessor, "DomainLabels.validate.isNull");
int len = labels.length();
if(len==0) return new InvalidResult(ApplicationResources.accessor, "DomainLabels.validate.empty");
if(len>DomainName.MAX_LENGTH) return new InvalidResult(ApplicationResources.accessor, "DomainLabels.validate.tooLong", DomainName.MAX_LENGTH, len);
int labelStart = 0;
for(int pos=0; pos interned = new ConcurrentHashMap<>();
/**
* If labels is null, returns null.
*/
public static DomainLabels valueOf(String labels) throws ValidationException {
if(labels==null) return null;
//DomainLabels existing = interned.get(labels);
//return existing!=null ? existing : new DomainLabels(labels);
return new DomainLabels(labels);
}
private String labels;
private String lowerLabels;
private DomainLabels(String labels) throws ValidationException {
this(labels, labels.toLowerCase(Locale.ROOT));
}
private DomainLabels(String labels, String lowerLabels) throws ValidationException {
this.labels = labels;
this.lowerLabels = lowerLabels;
validate();
}
private void validate() throws ValidationException {
ValidationResult result = validate(labels);
if(!result.isValid()) throw new ValidationException(result);
}
@Override
public boolean equals(Object O) {
return
O!=null
&& O instanceof DomainLabels
&& lowerLabels.equals(((DomainLabels)O).lowerLabels)
;
}
@Override
public int hashCode() {
return lowerLabels.hashCode();
}
/**
* Sorts by right-most label, then next to the left, then ...
*/
@Override
public int compareTo(DomainLabels other) {
if(this==other) return 0;
return DomainName.compareLabels(labels, other.labels);
}
@Override
public String toString() {
return labels;
}
/**
* Gets the lower-case form of the labels. If two different labels are
* interned and their toLowerCase is the same String instance, then they are
* equal in case-insensitive manner.
*/
public String toLowerCase() {
return lowerLabels;
}
/**
* Interns this set of labels much in the same fashion as String.intern()
.
*
* @see String#intern()
*/
@Override
public DomainLabels intern() {
try {
DomainLabels existing = interned.get(labels);
if(existing==null) {
String internedLabels = labels.intern();
String internedLowerLabels = lowerLabels.intern();
DomainLabels addMe = labels==internedLabels && lowerLabels==internedLowerLabels ? this : new DomainLabels(internedLabels);
existing = interned.putIfAbsent(internedLabels, addMe);
if(existing==null) existing = addMe;
}
return existing;
} catch(ValidationException err) {
// Should not fail validation since original object passed
throw new AssertionError(err.getMessage());
}
}
@Override
public com.aoindustries.aoserv.client.dto.DomainLabels getDto() {
return new com.aoindustries.aoserv.client.dto.DomainLabels(labels);
}
//
private static final long serialVersionUID = -2681659044454796989L;
public DomainLabels() {
}
@Override
public long getSerialVersionUID() {
return serialVersionUID;
}
@Override
public void writeExternal(ObjectOutput out) throws IOException {
FastObjectOutput fastOut = FastObjectOutput.wrap(out);
try {
fastOut.writeFastUTF(labels);
} finally {
fastOut.unwrap();
}
}
@Override
public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
if(labels!=null) throw new IllegalStateException();
FastObjectInput fastIn = FastObjectInput.wrap(in);
try {
labels = fastIn.readFastUTF();
lowerLabels = labels.toLowerCase(Locale.ROOT);
} finally {
fastIn.unwrap();
}
}
@Override
public void validateObject() throws InvalidObjectException {
try {
validate();
} catch(ValidationException err) {
InvalidObjectException newErr = new InvalidObjectException(err.getMessage());
newErr.initCause(err);
throw newErr;
}
}
//
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy