
com.github.helenusdriver.persistence.SetUUIDTokenizer Maven / Gradle / Ivy
Show all versions of api Show documentation
/*
* Copyright (C) 2015-2015 The Helenus Driver Project 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 com.github.helenusdriver.persistence;
import java.lang.reflect.Field;
import java.io.IOException;
import java.util.LinkedHashSet;
import java.util.Set;
import java.util.UUID;
import org.apache.commons.lang3.StringUtils;
/**
* The SetUUIDTokenizer
class provides an implementation for the
* {@link Persister} interface that converts a set of UUIDs into a string
* where all elements of the set are separated by a separator string.
*
* The SetUUIDTokenizer
expects one argument configured on the
* {@link Persisted} annotation. This argument is the separator string to be
* used when joining and splitting strings.
*
* @copyright 2015-2015 The Helenus Driver Project Authors
*
* @author The Helenus Driver Project Authors
* @version 1 - Jan 15, 2015 - paouelle - Creation
*
* @since 1.0
*/
@SuppressWarnings("unchecked")
public class SetUUIDTokenizer implements Persister, String> {
/**
* Holds a dummy field of type Set.
*
* @author paouelle
*/
@SuppressWarnings("unused")
private static final Set SET = null;
/**
* Holds the the class for Set.
*
* @author paouelle
*/
private static final Class> SET_CLASS;
static {
// we use some magic to extract the class object for Set
try {
final Field f = SetUUIDTokenizer.class.getDeclaredField("SET");
SET_CLASS = (Class>)f.getType();
} catch (Exception e) {
throw new Error(e);
}
}
/**
* Holds the separator string.
*
* @author paouelle
*/
private final String separator;
/**
* Instantiates a default SetUUIDTokenizer
object which will use
* the "," as a separator.
*
* @author paouelle
*/
public SetUUIDTokenizer() {
this.separator = ",";
}
/**
* Instantiates a new SetUUIDTokenizer
object.
*
* @author paouelle
*
* @param args the list of arguments
* @throws NullPointerException if args
is null
* @throws IllegalArgumentException if the arguments are not composed of a
* single argument
*/
public SetUUIDTokenizer(String... args) {
org.apache.commons.lang3.Validate.notNull(args, "invalid null args");
org.apache.commons.lang3.Validate.isTrue(
args.length == 1,
"invalid number of arguments provided: %d",
args.length
);
this.separator = args[0];
}
/**
* {@inheritDoc}
*
* @author paouelle
*
* @see com.github.helenusdriver.persistence.Persister#getDecodedClass()
*/
@Override
public Class> getDecodedClass() {
return SetUUIDTokenizer.SET_CLASS;
}
/**
* {@inheritDoc}
*
* @author paouelle
*
* @see com.github.helenusdriver.persistence.Persister#getPersistedClass()
*/
@Override
public Class getPersistedClass() {
return String.class;
}
/**
* {@inheritDoc}
*
* @author paouelle
*
* @see com.github.helenusdriver.persistence.Persister#encode(java.lang.Object)
*/
@Override
public String encode(Set set) {
return StringUtils.join(set, separator);
}
/**
* {@inheritDoc}
*
* @author paouelle
*
* @see com.github.helenusdriver.persistence.Persister#decode(java.lang.Object)
*/
@Override
public Set decode(String s) throws IOException {
final String[] as = StringUtils.splitByWholeSeparator(s, separator);
if (as == null) {
return null;
}
final Set set = new LinkedHashSet<>(as.length); // to maintain found order
for (final String se: as) {
try {
set.add(UUID.fromString(se));
} catch (IllegalArgumentException e) {
throw new IOException("unable to decode UUID: " + se, e);
}
}
return set;
}
}