#137 - Extracted TemplateVariable into separate class.

TempalteVariable is now a standalone class. Introduced TempalteVariables wrapper to allow easy collecting of TempalteVariable instances and rendering them in the shortest possible way.
This commit is contained in:
Oliver Gierke
2014-01-20 16:19:35 +01:00
parent 394fa4a009
commit b0cfaf7a62
6 changed files with 456 additions and 129 deletions

View File

@@ -0,0 +1,131 @@
/*
* Copyright 2014 the original author or authors.
*
* 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 org.springframework.hateoas;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import org.springframework.util.Assert;
/**
* Wrapper type for a collection of {@link TemplateVariable}.
*
* @author Oliver Gierke
*/
public class TemplateVariables implements Iterable<TemplateVariable> {
public static TemplateVariables NONE = new TemplateVariables(Collections.<TemplateVariable> emptyList());
private final List<TemplateVariable> variables;
/**
* Creates a new {@link TemplateVariables} for the given {@link TemplateVariable}s.
*
* @param variables must not be {@literal null}.
*/
public TemplateVariables(TemplateVariable... variables) {
this(Arrays.asList(variables));
}
/**
* Creates a new {@link TemplateVariables} for the given {@link TemplateVariable}s.
*
* @param variables must not be {@literal null}.
*/
public TemplateVariables(List<TemplateVariable> variables) {
Assert.notNull(variables, "Template variables must not be null!");
this.variables = Collections.unmodifiableList(variables);
}
/**
* Concatenates the given {@link TemplateVariable}s to the current one.
*
* @param variables must not be {@literal null}.
* @return
*/
public TemplateVariables concat(TemplateVariable... variables) {
return concat(Arrays.asList(variables));
}
/**
* Concatenates the given {@link TemplateVariable}s to the current one.
*
* @param variables must not be {@literal null}.
* @return
*/
public TemplateVariables concat(Collection<TemplateVariable> variables) {
List<TemplateVariable> result = new ArrayList<TemplateVariable>(this.variables.size() + variables.size());
result.addAll(this.variables);
result.addAll(variables);
return new TemplateVariables(result);
}
/**
* Concatenates the given {@link TemplateVariables} to the current one.
*
* @param variables must not be {@literal null}.
* @return
*/
public TemplateVariables concat(TemplateVariables variables) {
return concat(variables.variables);
}
/*
* (non-Javadoc)
* @see java.lang.Iterable#iterator()
*/
@Override
public Iterator<TemplateVariable> iterator() {
return this.variables.iterator();
}
/*
* (non-Javadoc)
* @see java.lang.Object#toString()
*/
public String toString() {
if (variables.isEmpty()) {
return "";
}
StringBuilder builder = new StringBuilder();
TemplateVariable previous = null;
for (TemplateVariable variable : variables) {
if (previous == null) {
builder.append("{").append(variable.getType().toString());
} else if (!previous.isOfSameTypeAs(variable)) {
builder.append("}{").append(variable.getType().toString());
} else {
builder.append(",");
}
previous = variable;
builder.append(variable.getName());
}
return builder.append("}").toString();
}
}