Sitelet https://github.com/KrailOrg/q3c-util/commit/c5ea61b600a4fc476792e0deb50200f85da2cc37
Skip to content

Commit c5ea61b

Browse files
author
David Sowerby
committed
Moved data converts in from Krail core
1 parent 347ca94 commit c5ea61b

11 files changed

Lines changed: 456 additions & 2 deletions

‎build.gradle‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ apply plugin: 'uk.q3c.kaytee'
2020
group='uk.q3c.util'
2121

2222
kaytee {
23-
baseVersion = "0.8.0.2"
23+
baseVersion = "0.8.1.0"
2424
}
2525

2626
bintray {
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/*
2+
*
3+
* * Copyright (c) 2016. David Sowerby
4+
* *
5+
* * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
6+
* * the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
7+
* *
8+
* * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
9+
* * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
10+
* * specific language governing permissions and limitations under the License.
11+
*
12+
*/
13+
14+
package uk.q3c.util.data
15+
16+
import com.google.common.base.Preconditions.checkNotNull
17+
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings
18+
import org.apache.commons.lang3.ClassUtils
19+
20+
/**
21+
* Converts an Class to a String representation and back.
22+
*
23+
*
24+
* Created by David Sowerby on 27/06/15.
25+
*/
26+
class ClassConverter : DataConverter<Class<*>> {
27+
/**
28+
* {@inheritDoc}
29+
*/
30+
@SuppressFBWarnings("EXS_EXCEPTION_SOFTENING_NO_CONSTRAINTS")
31+
override fun convertToModel(value: String): Class<*> {
32+
checkNotNull(value)
33+
try {
34+
return ClassUtils.getClass(this.javaClass
35+
.classLoader, value)
36+
} catch (e: Exception) {
37+
val msg = ("Failed to convert String '$value' to Class")
38+
throw ConversionException(msg, e)
39+
}
40+
41+
}
42+
43+
44+
override fun convertToString(value: Class<*>): String {
45+
checkNotNull(value)
46+
return value.canonicalName
47+
}
48+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/*
2+
* Copyright (c) 2015. David Sowerby
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
5+
* the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
6+
*
7+
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
8+
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
9+
* specific language governing permissions and limitations under the License.
10+
*/
11+
12+
package uk.q3c.util.data;
13+
14+
/**
15+
* Created by David Sowerby on 07/08/15.
16+
*/
17+
public class ConversionException extends RuntimeException {
18+
public ConversionException(String msg, Exception e) {
19+
super(msg, e);
20+
}
21+
22+
public ConversionException(String msg) {
23+
super(msg);
24+
}
25+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/*
2+
* Copyright (c) 2015. David Sowerby
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
5+
* the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
6+
*
7+
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
8+
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
9+
* specific language governing permissions and limitations under the License.
10+
*/
11+
12+
package uk.q3c.util.data;
13+
14+
public class ConverterException extends RuntimeException {
15+
public ConverterException(String msg) {
16+
super(msg);
17+
}
18+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/*
2+
* Copyright (c) 2015. David Sowerby
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
5+
* the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
6+
*
7+
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
8+
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
9+
* specific language governing permissions and limitations under the License.
10+
*/
11+
12+
package uk.q3c.util.data;
13+
14+
import javax.annotation.Nonnull;
15+
16+
/**
17+
* Created by David Sowerby on 06/08/15.
18+
*/
19+
public interface DataConverter<V> {
20+
21+
V convertToModel(String value) throws ConversionException;
22+
23+
String convertToString(@Nonnull V value);
24+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/*
2+
*
3+
* * Copyright (c) 2016. David Sowerby
4+
* *
5+
* * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
6+
* * the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
7+
* *
8+
* * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
9+
* * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
10+
* * specific language governing permissions and limitations under the License.
11+
*
12+
*/
13+
14+
package uk.q3c.util.data;
15+
16+
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
17+
import org.apache.commons.lang3.ClassUtils;
18+
19+
import javax.annotation.Nonnull;
20+
21+
import static com.google.common.base.Preconditions.*;
22+
23+
/**
24+
* Converts an Enum in full - that is, the class and constant as a single String.
25+
* <p>
26+
* Created by David Sowerby on 27/06/15.
27+
*/
28+
public class EnumConverter implements DataConverter<Enum> {
29+
/**
30+
* {@inheritDoc}
31+
*/
32+
@SuppressFBWarnings("EXS_EXCEPTION_SOFTENING_NO_CONSTRAINTS")
33+
@Override
34+
public Enum convertToModel(@Nonnull String value) throws ConversionException {
35+
checkNotNull(value);
36+
//slight mis-use of ClassUtils - it is just about the structure of the String
37+
String constantName = ClassUtils.getShortClassName(value);
38+
String className = ClassUtils.getPackageName(value);
39+
40+
try {
41+
@SuppressWarnings("unchecked") final Class<? extends Enum> clazz = (Class<? extends Enum>) Class.forName(className);
42+
return Enum.valueOf(clazz, constantName);
43+
} catch (Exception e) {
44+
String msg = "Failed to convert String '" + value + "' to Enum";
45+
throw new ConversionException(msg, e);
46+
}
47+
}
48+
49+
50+
@Override
51+
public String convertToString(@Nonnull Enum value) {
52+
checkNotNull(value);
53+
return value.getClass()
54+
.getName() + '.' + value.name();
55+
56+
}
57+
}

src/main/java/uk/q3c/util/collection/AnnotationList.java renamed to src/main/java/uk/q3c/util/data/collection/AnnotationList.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
*
1212
*/
1313

14-
package uk.q3c.util.collection;
14+
package uk.q3c.util.data.collection;
1515

1616
import com.google.common.collect.ImmutableList;
1717
import org.apache.commons.collections4.ListUtils;
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
/*
2+
*
3+
* * Copyright (c) 2016. David Sowerby
4+
* *
5+
* * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
6+
* * the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
7+
* *
8+
* * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
9+
* * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
10+
* * specific language governing permissions and limitations under the License.
11+
*
12+
*/
13+
14+
package uk.q3c.util.data.collection;
15+
16+
import com.google.common.base.Splitter;
17+
import com.google.common.collect.ImmutableList;
18+
import org.apache.commons.lang3.StringEscapeUtils;
19+
import uk.q3c.util.data.ClassConverter;
20+
import uk.q3c.util.data.ConversionException;
21+
22+
import javax.annotation.Nonnull;
23+
import java.lang.annotation.Annotation;
24+
import java.util.ArrayList;
25+
import java.util.List;
26+
27+
import static com.google.common.base.Preconditions.*;
28+
29+
30+
/**
31+
* Converts a OptionList<?><X> where X is an element with its own Converter, to and from a String representation (specifically a single String with elements
32+
* quoted and comma-separated as defined by {@link StringEscapeUtils#escapeCsv(String)})
33+
* <p>
34+
* Created by David Sowerby on 04/08/15.
35+
*/
36+
public class AnnotationListConverter {
37+
38+
public final static String separator = "~~";
39+
40+
public AnnotationListConverter() {
41+
super();
42+
}
43+
44+
/**
45+
* Converts {@code model} to String
46+
*
47+
* @param model the model to convert
48+
* @return Comma separated String representation of the list contents
49+
*/
50+
public String convertToString(@Nonnull AnnotationList model) {
51+
checkNotNull(model);
52+
if (model.isEmpty()) {
53+
return "";
54+
}
55+
StringBuilder buf = new StringBuilder();
56+
ImmutableList<Class<? extends Annotation>> modelList = model.getList();
57+
ClassConverter classConverter = new ClassConverter();
58+
boolean first = true;
59+
for (Class<? extends Annotation> e : modelList) {
60+
if (!first) {
61+
buf.append(separator);
62+
} else {
63+
first = false;
64+
}
65+
buf.append(StringEscapeUtils.escapeCsv(classConverter.convertToString(e)));
66+
}
67+
return buf.toString();
68+
}
69+
70+
/**
71+
* Converts String representation, {@code value}, to model
72+
*
73+
* @param value the String to convert
74+
* @return Comma separated String representation of the list contents
75+
* @throws ConversionException if conversion fails
76+
*/
77+
public AnnotationList convertToModel(@Nonnull String value) {
78+
checkNotNull(value);
79+
if (value.isEmpty()) {
80+
return new AnnotationList();
81+
}
82+
final List<String> strings = Splitter.on(separator)
83+
.splitToList(value);
84+
List<Class<? extends Annotation>> elementList = new ArrayList<>();
85+
strings.forEach(s -> {
86+
String unescaped = StringEscapeUtils.unescapeCsv(s);
87+
Class<?> element = new ClassConverter().convertToModel(unescaped);
88+
if (Annotation.class.isAssignableFrom(element)) {
89+
//noinspection unchecked
90+
elementList.add((Class<? extends Annotation>) element);
91+
} else {
92+
throw new ConversionException("Class " + element + " is not an Annotation");
93+
}
94+
});
95+
return new AnnotationList(elementList);
96+
}
97+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/*
2+
*
3+
* * Copyright (c) 2016. David Sowerby
4+
* *
5+
* * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
6+
* * the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
7+
* *
8+
* * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
9+
* * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
10+
* * specific language governing permissions and limitations under the License.
11+
*
12+
*/
13+
14+
package uk.q3c.util.data
15+
16+
import spock.lang.Specification
17+
import uk.q3c.util.forest.BasicForest
18+
19+
/**
20+
*
21+
* Created by David Sowerby on 08/08/15.
22+
*/
23+
class ClassConverterTest extends Specification {
24+
25+
ClassConverter converter
26+
27+
def setup() {
28+
converter = new ClassConverter()
29+
}
30+
31+
def "round trip "() {
32+
33+
expect:
34+
converter.convertToModel(converter.convertToString(BasicForest)).equals(BasicForest)
35+
}
36+
37+
def "convert invalid class name throws ConversionException"() {
38+
when:
39+
40+
converter.convertToModel("rubbish")
41+
42+
then:
43+
44+
thrown ConversionException
45+
}
46+
}

0 commit comments

Comments
 (0)