Commit 2cb88c13 authored by Matija Obreza's avatar Matija Obreza
Browse files

MCPDUtil#toStrings fixed and renamed to #toList

parent 1905accc
......@@ -113,7 +113,7 @@ public class AccessionDetails {
ad.available = accession.getAvailability();
ad.historic = accession.getHistoric();
ad.orgCty = Country.from(accession.getCountryOfOrigin());
ad.duplSite = MCPDUtil.toStrings(accession.getDuplSite());
ad.duplSite = MCPDUtil.toList(accession.getDuplSite());
ad.institute = Institute.from(accession.getInstitute());
ad.mlsStatus = accession.getMlsStatus();
ad.art15 = accession.getInTrust();
......
......@@ -31,7 +31,7 @@ public class Collect {
Collect c = new Collect();
if (StringUtils.isNotBlank(collect.getCollCode()))
c.collCode = new HashSet<String>(MCPDUtil.toStrings(collect.getCollCode()));
c.collCode = new HashSet<String>(MCPDUtil.toList(collect.getCollCode()));
c.collDate = StringUtils.defaultIfBlank(collect.getCollDate(), null);
c.collInstAddr = StringUtils.defaultIfBlank(collect.getCollInstAddress(), null);
c.collMissId = StringUtils.defaultIfBlank(collect.getCollMissId(), null);
......
......@@ -16,50 +16,61 @@
package org.genesys2.util;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import org.apache.commons.lang.StringUtils;
public class MCPDUtil {
static Pattern mcpdSplit = Pattern.compile("\\w*;\\w*");
static Pattern mcpdSplit = Pattern.compile("\\s*;\\s*");
public static String toMcpdArray(List<Integer> integers) {
if (integers == null || integers.size() == 0)
/**
* Convert a List to MCPD "array string" with semicolon as separator.
*
* @param integers
* @return
*/
public static String toMcpdArray(List<?> objects) {
if (objects == null || objects.size() == 0)
return null;
String s = "";
for (Integer i : integers) {
if (i == null)
continue;
if (s.length() > 0)
s += ";";
s += i;
}
return StringUtils.defaultIfBlank(s, null);
StringBuilder sb = new StringBuilder();
objects.stream().filter(i -> i != null).forEach(i -> {
if (sb.length() > 0)
sb.append(";");
sb.append(i);
});
return StringUtils.defaultIfBlank(sb.toString(), null);
}
/**
* Convert an MCPD "array" (e.g. "10;20") to a {@link List} of Integers,
* excluding blank and null elements.
*
* @throws NumberFormatException
* if the input string contains non-numeric elements.
*/
public static List<Integer> toIntegers(String ints) {
if (StringUtils.isBlank(ints))
return null;
List<Integer> is = new ArrayList<Integer>();
for (String i : mcpdSplit.split(ints)) {
if (StringUtils.isBlank(i))
continue;
try {
is.add(Integer.parseInt(i));
} catch (NumberFormatException e) {
}
}
return is;
return Arrays.stream(mcpdSplit.split(ints)).filter(str -> StringUtils.isNotBlank(str)).map(i -> Integer.parseInt(i)).collect(Collectors.toList());
}
public static List<String> toStrings(String strings) {
/**
* Convert an MCPD "array string" (e.g. "NOR051;AUS033") to a {@link List},
* excluding blank and null elements.
*
* @param strings
* @return
*/
public static List<String> toList(String strings) {
if (StringUtils.isBlank(strings))
return null;
return Arrays.asList(mcpdSplit.split(strings));
return Arrays.stream(mcpdSplit.split(strings)).filter(str -> StringUtils.isNotBlank(str)).collect(Collectors.toList());
}
}
/**
* Copyright 2016 Global Crop Diversity Trust
*
* 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.genesys2.server.model.impl;
import static org.hamcrest.CoreMatchers.nullValue;
import static org.hamcrest.Matchers.*;
import static org.junit.Assert.assertThat;
import java.util.ArrayList;
import java.util.List;
import org.genesys2.util.MCPDUtil;
import org.junit.Test;
public class MCPDUtilTest {
@Test
public void testMcpdArray() {
assertThat("Result should be null", MCPDUtil.toMcpdArray(null), is(nullValue()));
List<Integer> ints = new ArrayList<Integer>();
assertThat("Result should be null", MCPDUtil.toMcpdArray(ints), is(nullValue()));
ints.add(1);
assertThat("Result incorrect", MCPDUtil.toMcpdArray(ints), equalTo("1"));
ints.add(2);
assertThat("Result incorrect", MCPDUtil.toMcpdArray(ints), equalTo("1;2"));
ints.add(3);
assertThat("Result incorrect", MCPDUtil.toMcpdArray(ints), equalTo("1;2;3"));
ints.remove(0);
assertThat("Result incorrect", MCPDUtil.toMcpdArray(ints), equalTo("2;3"));
ints.add(null);
assertThat("Result incorrect", MCPDUtil.toMcpdArray(ints), equalTo("2;3"));
ints.add(4);
assertThat("Result incorrect", MCPDUtil.toMcpdArray(ints), equalTo("2;3;4"));
}
@Test
public void testToStrings() {
assertThat("List should be null", MCPDUtil.toList(null), is(nullValue()));
assertThat("List should have 1 element", MCPDUtil.toList("NOR051;"), hasSize(equalTo(1)));
assertThat("Missing element", MCPDUtil.toList("NOR051;"), contains("NOR051"));
assertThat("List should have 1 element", MCPDUtil.toList(";NOR051;"), hasSize(equalTo(1)));
assertThat("Missing element", MCPDUtil.toList(";NOR051;"), contains("NOR051"));
assertThat("List should have 1 element", MCPDUtil.toList("NOR051;;"), hasSize(equalTo(1)));
assertThat("Missing element", MCPDUtil.toList("NOR051;;"), contains("NOR051"));
assertThat("List should have 2 elements", MCPDUtil.toList("AUS053;NOR051"), hasSize(equalTo(2)));
assertThat("Missing element", MCPDUtil.toList("AUS053;NOR051"), contains("AUS053", "NOR051"));
assertThat("List should have 2 elements", MCPDUtil.toList("; ; AUS053;NOR051"), hasSize(equalTo(2)));
assertThat("Missing element", MCPDUtil.toList("; ; AUS053;NOR051"), contains("AUS053", "NOR051"));
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment