org.ovirt.api.metamodel.concepts.Name Maven / Gradle / Ivy
/*
Copyright (c) 2015-2017 Red Hat, Inc.
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.ovirt.api.metamodel.concepts;
import static java.lang.String.join;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.stream.Stream;
/**
* This class represents a name formed of multiple words. It is intended to simplify the use of different strategies for
* representing names as strings, like using different separators or using camel case. The words that form the name are
* stored separated, so there is no need to parse the name each time that the words are needed.
*/
public class Name implements Comparable, Serializable {
/**
* The list of words of this name.
*/
private ArrayList words = new ArrayList<>(1);
public Name() {
super();
}
@SuppressWarnings("unchecked")
/**
* Copy Constructor
*/
public Name(Name nameToClone) {
super();
words = (ArrayList)nameToClone.words.clone();
}
/**
* Creates a new word using the given list of words.
*
* @param theWords the words that will be used to construct the name
*/
public Name(String... theWords) {
words.ensureCapacity(theWords.length);
for (String theWord : theWords) {
words.add(theWord.toLowerCase());
}
}
/**
* Creates a new word using the given list of words.
*
* @param theWords the words that will be used to construct the name
*/
public Name(List theWords) {
words.ensureCapacity(theWords.size());
words.addAll(theWords);
}
/**
* Returns the list of words of this name. The returned list is a copy of the one used internally, so any changes
* to it won't have any effect on this name, and changes in the name won't affect the returned list.
*/
public List getWords() {
return new ArrayList<>(words);
}
/**
* Replaces the list of words of this name. The list passed as parameter isn't modified or referenced, instead
* its contents are copied, so any later changes to it won't have any effect on this name.
*
* @param newWords the list of words that will replace the words of this name
*/
public void setWords(List newWords) {
words.clear();
for (String newWord : newWords) {
words.add(newWord.toLowerCase());
}
}
/**
* Replaces the element at the specified position in this list of words with the specified element
* @param index the position in the list
* @param word the replacement word
*/
public void setWord(int index, String word) {
words.set(index, word);
}
/**
* Adds a new word to the end of the list of words of this name.
*
* @param newWord the world that will be added
*/
public void addWord(String newWord) {
words.add(newWord.toLowerCase());
}
/**
* Adds a new word to the end of the list of words of this name.
*
* @param newWord the world that will be added
*/
public void addWords(List words) {
this.words.addAll(words);
}
/**
* Returns a sequential {@code Stream} with the list of words of the name as source.
*/
public Stream words() {
return words.stream();
}
/**
* Returns a string representation of this name, consisting on the list of words of the name separated by underscores.
*/
@Override
public String toString() {
return join("_", words);
}
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (!(obj instanceof Name)) {
return false;
}
Name other = (Name) obj;
return Objects.equals(words, other.words);
}
@Override
public int hashCode() {
return Objects.hashCode(words);
}
/**
* Compares two names lexicographically.
*/
@Override
public int compareTo(Name that) {
int thisLength = this.words.size();
int thatLength = that.words.size();
int minLength = Math.min(thisLength, thatLength);
for (int i = 0; i < minLength; i++) {
String thisWord = this.words.get(i);
String thatWord = that.words.get(i);
int result = thisWord.compareTo(thatWord);
if (result != 0) {
return result;
}
}
return Integer.compare(thisLength, thatLength);
}
}