diff --git a/src/main/java/walkingkooka/spreadsheet/compare/SpreadsheetComparatorNameList.java b/src/main/java/walkingkooka/spreadsheet/compare/SpreadsheetComparatorNameList.java new file mode 100644 index 000000000..39352fcd8 --- /dev/null +++ b/src/main/java/walkingkooka/spreadsheet/compare/SpreadsheetComparatorNameList.java @@ -0,0 +1,123 @@ +/* + * Copyright 2019 Miroslav Pokorny (github.com/mP1) + * + * 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 walkingkooka.spreadsheet.compare; + +import org.junit.jupiter.api.Test; +import walkingkooka.collect.list.ImmutableListDefaults; +import walkingkooka.collect.list.Lists; +import walkingkooka.plugin.PluginNameLike; +import walkingkooka.text.CharacterConstant; +import walkingkooka.text.HasText; +import walkingkooka.tree.json.JsonNode; +import walkingkooka.tree.json.marshall.JsonNodeContext; +import walkingkooka.tree.json.marshall.JsonNodeMarshallContext; +import walkingkooka.tree.json.marshall.JsonNodeUnmarshallContext; + +import java.util.AbstractList; +import java.util.List; +import java.util.Objects; + +/** + * An immutable list of {@link SpreadsheetComparatorName}. This can be used to hold a list of names for sorting. + */ +public final class SpreadsheetComparatorNameList extends AbstractList + implements ImmutableListDefaults, + HasText { + + /** + * Parses a CSV string of {@link SpreadsheetComparatorName names} into a {@link SpreadsheetComparatorNameList}. + */ + public static SpreadsheetComparatorNameList parse(final String text) { + return PluginNameLike.parse( + text, + SpreadsheetComparatorName::with, + SpreadsheetComparatorNameList::with + ); + } + + /** + * Factory that creates a {@link SpreadsheetComparatorNameList} from the list of {@link SpreadsheetComparatorName names}. + */ + public static SpreadsheetComparatorNameList with(final List names) { + Objects.requireNonNull(names, "names"); + + return names instanceof SpreadsheetComparatorNameList ? + (SpreadsheetComparatorNameList) names : + new SpreadsheetComparatorNameList( + Lists.immutable(names) + ); + } + + private SpreadsheetComparatorNameList(final List names) { + this.names = names; + } + + @Override + public SpreadsheetComparatorName get(int index) { + return this.names.get(index); + } + + @Override + public int size() { + return this.names.size(); + } + + private final List names; + + @Override + public SpreadsheetComparatorNameList setElements(final List names) { + final SpreadsheetComparatorNameList copy = with(names); + return this.equals(copy) ? + this : + copy; + } + + // HasText.......................................................................................................... + + @Test + public String text() { + return CharacterConstant.COMMA.toSeparatedString( + this, + SpreadsheetComparatorName::value + ); + } + + // json............................................................................................................. + + static SpreadsheetComparatorNameList unmarshall(final JsonNode node, + final JsonNodeUnmarshallContext context) { + return parse(node.stringOrFail()); + } + + private JsonNode marshall(final JsonNodeMarshallContext context) { + return JsonNode.string( + this.text() + ); + } + + static { + SpreadsheetComparatorName.DATE.toString(); + + JsonNodeContext.register( + JsonNodeContext.computeTypeName(SpreadsheetComparatorNameList.class), + SpreadsheetComparatorNameList::unmarshall, + SpreadsheetComparatorNameList::marshall, + SpreadsheetComparatorNameList.class + ); + } +} diff --git a/src/test/java/walkingkooka/spreadsheet/compare/SpreadsheetComparatorNameListTest.java b/src/test/java/walkingkooka/spreadsheet/compare/SpreadsheetComparatorNameListTest.java new file mode 100644 index 000000000..9ab0cc40c --- /dev/null +++ b/src/test/java/walkingkooka/spreadsheet/compare/SpreadsheetComparatorNameListTest.java @@ -0,0 +1,159 @@ +/* + * Copyright 2019 Miroslav Pokorny (github.com/mP1) + * + * 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 walkingkooka.spreadsheet.compare; + +import org.junit.jupiter.api.Test; +import walkingkooka.collect.list.ImmutableListTesting; +import walkingkooka.collect.list.ListTesting2; +import walkingkooka.collect.list.Lists; +import walkingkooka.reflect.ClassTesting; +import walkingkooka.reflect.JavaVisibility; +import walkingkooka.tree.json.JsonNode; +import walkingkooka.tree.json.marshall.JsonNodeMarshallingTesting; +import walkingkooka.tree.json.marshall.JsonNodeUnmarshallContext; + +import static org.junit.jupiter.api.Assertions.assertSame; +import static org.junit.jupiter.api.Assertions.assertThrows; + +public class SpreadsheetComparatorNameListTest implements ListTesting2, + ClassTesting, + ImmutableListTesting, + JsonNodeMarshallingTesting { + + private final static SpreadsheetComparatorName DATE1 = SpreadsheetComparatorName.DATE; + + private final static SpreadsheetComparatorName NUMBER2 = SpreadsheetComparatorName.NUMBER; + + @Test + public void testWithNullFails() { + assertThrows( + NullPointerException.class, + () -> SpreadsheetComparatorNameList.with(null) + ); + } + + @Test + public void testWithDoesntDoubleWrap() { + final SpreadsheetComparatorNameList list = this.createList(); + assertSame( + list, + SpreadsheetComparatorNameList.with(list) + ); + } + + @Test + public void testGet() { + this.getAndCheck( + this.createList(), + 0, // index + DATE1 // expected + ); + } + + @Test + public void testGet2() { + this.getAndCheck( + this.createList(), + 1, // index + NUMBER2 // expected + ); + } + + @Test + public void testSetFails() { + this.setFails( + this.createList(), + 0, // index + DATE1 // expected + ); + } + + @Test + public void testRemoveIndexFails() { + final SpreadsheetComparatorNameList list = this.createList(); + + this.removeIndexFails( + list, + 0 + ); + } + + @Test + public void testRemoveElementFails() { + final SpreadsheetComparatorNameList list = this.createList(); + + this.removeFails( + list, + list.get(0) + ); + } + + @Override + public SpreadsheetComparatorNameList createList() { + return SpreadsheetComparatorNameList.with( + Lists.of( + DATE1, + NUMBER2 + ) + ); + } + + // class............................................................................................................ + + @Override + public Class type() { + return SpreadsheetComparatorNameList.class; + } + + @Override + public JavaVisibility typeVisibility() { + return JavaVisibility.PUBLIC; + } + + // Json............................................................................................................. + + @Test + public void testMarshall() { + this.marshallAndCheck( + this.createList(), + "\"date,number\"" + ); + } + + @Test + public void testUnmarshall() { + this.unmarshallAndCheck( + "\"date,number\"", + this.createList() + ); + } + + @Override + public SpreadsheetComparatorNameList unmarshall(final JsonNode json, + final JsonNodeUnmarshallContext context) { + return SpreadsheetComparatorNameList.unmarshall( + json, + context + ); + } + + @Override + public SpreadsheetComparatorNameList createJsonNodeMarshallingValue() { + return this.createList(); + } +}