Commit c7294d17 authored by Matija Obreza's avatar Matija Obreza

Remark parser

parent 5abe5f80
......@@ -16,11 +16,16 @@
package org.genesys2.client.oauth.api.accession;
import java.util.Arrays;
import java.util.stream.Collectors;
/**
* The Class Remark.
*/
public class Remark {
private static final Remark[] REMARK_ARRAY = new Remark[] {};
/** The field name. */
private String fieldName;
......@@ -63,4 +68,23 @@ public class Remark {
this.remark = remark;
}
public static Remark[] parse(String source) {
if (source == null || source.trim().length() == 0)
return null;
Remark[] remarks = Arrays.stream(source.trim().split(";")).map(r -> r.trim()).filter(r -> r.length() > 0).map(r -> {
Remark remark = new Remark();
String[] s = r.split(":", 2);
if (s.length == 1) {
remark.remark = s[0].trim();
} else {
remark.fieldName = s[0].trim();
remark.remark = s[1].trim();
}
return remark;
}).collect(Collectors.toList()).toArray(REMARK_ARRAY);
return remarks.length == 0 ? null : remarks;
}
}
/*
* Copyright 2017 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.geneys2.client.oauth;
import static org.hamcrest.CoreMatchers.*;
import static org.hamcrest.MatcherAssert.*;
import org.genesys2.client.oauth.api.accession.Remark;
import org.junit.Test;
public class RemarkTest {
@Test
public void testRemarkParser() {
assertThat(Remark.parse(null), nullValue());
assertThat(Remark.parse(""), nullValue());
assertThat(Remark.parse(" "), nullValue());
assertThat(Remark.parse(" ;;"), nullValue());
assertThat(Remark.parse("Remark"), not(nullValue()));
Remark r = Remark.parse("FIELD:Remark")[0];
assertThat(r.getFieldName(), is("FIELD"));
assertThat(r.getRemark(), is("Remark"));
r = Remark.parse("FIELD :Remark")[0];
assertThat(r.getFieldName(), is("FIELD"));
assertThat(r.getRemark(), is("Remark"));
r = Remark.parse("FIELD: Remark")[0];
assertThat(r.getFieldName(), is("FIELD"));
assertThat(r.getRemark(), is("Remark"));
Remark[] rr = Remark.parse("FIELD: Remark;Test");
assertThat(rr.length, is(2));
assertThat(rr[0].getFieldName(), is("FIELD"));
assertThat(rr[0].getRemark(), is("Remark"));
assertThat(rr[1].getFieldName(), nullValue());
assertThat(rr[1].getRemark(), is("Test"));
}
}
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