Skip to content

Commit

Permalink
Merge pull request #5137 from mP1/feature/SpreadsheetComparatorNameList
Browse files Browse the repository at this point in the history
SpreadsheetComparatorNameList
  • Loading branch information
mP1 committed Sep 19, 2024
2 parents 04d46e4 + b321314 commit 4d10152
Show file tree
Hide file tree
Showing 2 changed files with 282 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -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<SpreadsheetComparatorName>
implements ImmutableListDefaults<SpreadsheetComparatorNameList, SpreadsheetComparatorName>,
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<SpreadsheetComparatorName> names) {
Objects.requireNonNull(names, "names");

return names instanceof SpreadsheetComparatorNameList ?
(SpreadsheetComparatorNameList) names :
new SpreadsheetComparatorNameList(
Lists.immutable(names)
);
}

private SpreadsheetComparatorNameList(final List<SpreadsheetComparatorName> 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<SpreadsheetComparatorName> names;

@Override
public SpreadsheetComparatorNameList setElements(final List<SpreadsheetComparatorName> 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
);
}
}
Original file line number Diff line number Diff line change
@@ -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<SpreadsheetComparatorNameList, SpreadsheetComparatorName>,
ClassTesting<SpreadsheetComparatorNameList>,
ImmutableListTesting<SpreadsheetComparatorNameList, SpreadsheetComparatorName>,
JsonNodeMarshallingTesting<SpreadsheetComparatorNameList> {

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<SpreadsheetComparatorNameList> 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();
}
}

0 comments on commit 4d10152

Please sign in to comment.