Skip to content

Commit

Permalink
Change the order of the id() method's parameters
Browse files Browse the repository at this point in the history
Such a modification makes current API more consistent by using the same order in id() and in create()
  • Loading branch information
echebbi committed Jan 24, 2018
1 parent 375f270 commit 8129192
Show file tree
Hide file tree
Showing 12 changed files with 69 additions and 71 deletions.
13 changes: 6 additions & 7 deletions src/main/java/fr/kazejiyu/generic/datatable/core/Columns.java
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ default Columns create(ColumnId<?> id) {
* @throws InconsistentColumnSizeException if ! rows.isEmpty()
*/
default Columns create(String header, Class<?> type) {
return create(type, header, Collections.emptyList());
return create(header, type, Collections.emptyList());
}

/**
Expand Down Expand Up @@ -253,18 +253,17 @@ default <N> Columns create(ColumnId<N> id, N... column) {
*/
@SuppressWarnings("unchecked")
default <N> Columns create(String header, Class<N> type, N... column) {
return create(type, header, Arrays.asList(column));
return create(header, type, Arrays.asList(column));
}

/**
* Creates a new {@code Column} from a given iterable.
*
* @param type
* The type of the elements in the new column.
* Must not be {@code null}.
* @param header
* The column's header.
* Must not be {@code null}.
* @param type
* The type of the elements in the new column.
* Must not be {@code null}.
* @param column
* The elements of the column.
* Must not be {@code null}.
Expand All @@ -276,7 +275,7 @@ default <N> Columns create(String header, Class<N> type, N... column) {
* @throws NullPointerException if any of the parameters is {@code null}.
* @throws InconsistentColumnSizeException if {@code column.size()} != {@code this.size()}
*/
<N> Columns create(Class<N> type, String header, Iterable<N> column);
<N> Columns create(String header, Class<N> type, Iterable<N> column);

/**
* Removes a column from the table.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public final class ColumnId<T> {

private final String header;

public ColumnId(Class<T> type, String header) {
public ColumnId(String header, Class<T> type) {
this.type = requireNonNull(type, "The type of a ColumnId must not be null");
this.header = requireNonNull(header, "The header of a ColumnId must not be null");
}
Expand Down Expand Up @@ -83,18 +83,17 @@ public boolean equals(ColumnId<?> other) {

/**
* Creates a new {@code ColumnId} with a specific type and header.
*
* @param type
* The class of the elements contained by the column.
* @param header
* The header of the column.
* @param type
* The class of the elements contained by the column.
*
* @return a new {@code ColumnId}
*
* @param <T> the type of the elements contained by the column
*/
public static <T> ColumnId<T> id(Class<T> type, String header) {
return new ColumnId<>(type, header);
public static <T> ColumnId<T> id(String header, Class<T> type) {
return new ColumnId<>(header, type);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ public int indexOf(final ColumnId<?> id) {
}

@Override
public <N> Columns create(final Class<N> type, final String header, final Iterable<N> column) {
public <N> Columns create(final String header, final Class<N> type, final Iterable<N> column) {
preconditions.assertIsAValidNewColumn(type, header, column);

Iterator<N> itElement = column.iterator();
Expand All @@ -165,14 +165,14 @@ public <N> Columns create(final Class<N> type, final String header, final Iterab
((ModifiableRow) row).add(itElement.next());
}

createLastColumn(id(type, header));
createLastColumn(id(header, type));
return this;
}

private void createLastColumn(ColumnId<?> id) {
int nextIndex = size();

idToIndex.put(id(id.type(), normalize(id.header())), nextIndex);
idToIndex.put(id(normalize(id.header()), id.type()), nextIndex);
headerToIndex.put(normalize(id.header()), nextIndex);
elements.add( new SimpleColumn<>(id, table) );
}
Expand Down
32 changes: 16 additions & 16 deletions src/test/java/fr/kazejiyu/generic/datatable/ColumnIdTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class ColumnIdTest {
void createId() {
type = String.class;
header = "Header";
id = id(type, header);
id = id(header, type);
}

// type()
Expand Down Expand Up @@ -62,12 +62,12 @@ void is_not_equal_to_a_non_columnid_object() {

@Test @DisplayName("is not equal to a similar id with different header")
void is_not_equal_to_a_similar_id_with_different_header() {
assertThat(id).isNotEqualTo(id(type, " " + header));
assertThat(id).isNotEqualTo(id(" " + header, type));
}

@Test @DisplayName("is not equal to a similar id with different type")
void is_not_equal_to_a_similar_id_with_different_type() {
assertThat(id).isNotEqualTo(id(this.getClass(), header));
assertThat(id).isNotEqualTo(id(header, this.getClass()));
}

@Test @DisplayName("is equal to self")
Expand All @@ -77,14 +77,14 @@ void is_equal_to_self() {

@Test @DisplayName("is equal to an identical id")
void is_equal_to_an_identical_id() {
assertThat(id).isEqualTo(id(type, header));
assertThat(id).isEqualTo(id(header, type));
}

@ParameterizedTest
@ValueSource(strings = {"HEADER", "header", "hEADER"})
@DisplayName("is equal to an identical id without case consideration")
void is_equal_to_an_identical_id_without_case_consideration(String otherHeader) {
ColumnId<String> str = id(String.class, otherHeader);
ColumnId<String> str = id(otherHeader, String.class);
SoftAssertions softly = new SoftAssertions();

softly.assertThat(s(str)).isEqualTo(str);
Expand All @@ -107,17 +107,17 @@ void has_the_same_hashCode_than_an_identical_column_of_strings_id() {

@Test @DisplayName("can create an array of column of strings with identical id")
void can_create_an_array_of_column_of_strings_with_identical_id() {
ColumnId<String> s1 = id(String.class, "headER1");
ColumnId<String> s2 = id(String.class, " headER 2");
ColumnId<String> s3 = id(String.class, " ");
ColumnId<String> s4 = id(String.class, "HEADER 4");
ColumnId<String> s1 = id("headER1", String.class);
ColumnId<String> s2 = id(" headER 2", String.class);
ColumnId<String> s3 = id(" ", String.class);
ColumnId<String> s4 = id("HEADER 4", String.class);

assertThat(s(s1, s2, s3, s4)).containsExactly(s(s1), s(s2), s(s3), s(s4));
}

@Test @DisplayName("is not equal to a similar column of strings id with a different header")
void is_not_equal_to_a_similar_column_of_strings_id_with_a_different_header() {
ColumnId<String> different = id(String.class, header + "#");
ColumnId<String> different = id(header + "#", String.class);
assertThat(different).isNotEqualTo(s(id));
assertThat(s(id)).isNotEqualTo(different);
}
Expand All @@ -126,7 +126,7 @@ void is_not_equal_to_a_similar_column_of_strings_id_with_a_different_header() {

@Test @DisplayName("can create an identical column of numbers id")
void can_create_an_identical_column_of_numbers_id() {
ColumnId<Double> dbl = id(Double.class, "double");
ColumnId<Double> dbl = id("double", Double.class);
SoftAssertions softly = new SoftAssertions();

softly.assertThat(n(dbl)).isEqualTo(dbl);
Expand All @@ -137,23 +137,23 @@ void can_create_an_identical_column_of_numbers_id() {

@Test @DisplayName("has the same hashCode than an identical column of numbers id")
void has_the_same_hash_code_than_an_identical_column_of_numbers_id() {
ColumnId<Double> dbl = id(Double.class, "double");
ColumnId<Double> dbl = id("double", Double.class);
assertThat(dbl.hashCode()).isEqualTo(n(dbl).hashCode());
}

@Test @DisplayName("is not equal to a similar column of numbers id with a different header")
void is_not_equal_to_a_similar_column_of_numbers_id_with_a_different_header() {
ColumnId<Double> nbr = id(Double.class, header);
ColumnId<Double> dbl = id(Double.class, header + "#");
ColumnId<Double> nbr = id(header, Double.class);
ColumnId<Double> dbl = id(header + "#", Double.class);

assertThat(nbr).isNotEqualTo(n(dbl));
assertThat(n(dbl)).isNotEqualTo(nbr);
}

@Test @DisplayName("has not the same hashCode than a different column of numbers id")
void has_not_the_same_hash_code_than_a_different_column_of_numbers_id() {
ColumnId<Double> nbr = id(Double.class, header);
ColumnId<Double> dbl = id(Double.class, header + "#");
ColumnId<Double> nbr = id(header, Double.class);
ColumnId<Double> dbl = id(header + "#", Double.class);

assertThat(dbl.hashCode()).isNotEqualTo(nbr.hashCode());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,9 +105,9 @@ void is_equal_to_a_new_datatable() {
class NonEmpty {
private Table people;

private final ColumnId<String> NAME = id(String.class, NAME_HEADER);
private final ColumnId<Integer> AGE = id(Integer.class, AGE_HEADER);
private final ColumnId<Integer> SEX = id(Integer.class, SEX_HEADER);
private final ColumnId<String> NAME = id(NAME_HEADER, String.class);
private final ColumnId<Integer> AGE = id(AGE_HEADER, Integer.class);
private final ColumnId<Integer> SEX = id(SEX_HEADER, Integer.class);

private static final String AGE_HEADER = "AGE";
private static final String NAME_HEADER = "name";
Expand Down Expand Up @@ -218,7 +218,7 @@ void can_filter_only_specific_columns() {
void throws_when_filtering_non_existing_ids() {
LinkedHashSet<ColumnId<?>> ids = new LinkedHashSet<>();
ids.add(NAME);
ids.add(id(String.class, "non existing"));
ids.add(id("non existing", String.class));
ids.add(SEX);

assertThatExceptionOfType(ColumnIdNotFoundException.class)
Expand Down
24 changes: 12 additions & 12 deletions src/test/java/fr/kazejiyu/generic/datatable/SimpleColumnsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ private ThrowingCallable addingTheSameHeaderTwice(Table table, String header) {

@Test @DisplayName("can create empty columns at the end")
void can_create_empty_columns_at_the_end() {
ColumnId<Integer> height = id(Integer.class, "height");
ColumnId<Integer> height = id("height", Integer.class);
empty.columns().create(height);
Column<?> last = empty.columns().last();

Expand Down Expand Up @@ -172,9 +172,9 @@ class NonEmptyTable {
private static final String NAME_HEADER = "name";
private static final String SEX_HEADER = "sEx";

private final ColumnId<Integer> AGE = id(Integer.class, AGE_HEADER);
private final ColumnId<String> NAME = id(String.class, NAME_HEADER);
private final ColumnId<String> SEX = id(String.class, SEX_HEADER);
private final ColumnId<Integer> AGE = id(AGE_HEADER, Integer.class);
private final ColumnId<String> NAME = id(NAME_HEADER, String.class);
private final ColumnId<String> SEX = id(SEX_HEADER, String.class);

@BeforeEach
void initializePeopleTable() {
Expand All @@ -200,7 +200,7 @@ void is_not_empty() {
void throws_when_creating_a_wrong_sized_column(List<Object> column) {
assertThatExceptionOfType(InconsistentColumnSizeException.class)
.as("creating a column of " + column.size() + " elements")
.isThrownBy(() -> people.columns().create(Object.class, "Illegal Column", column));
.isThrownBy(() -> people.columns().create("Illegal Column", Object.class, column));
}

@SuppressWarnings("unused")
Expand Down Expand Up @@ -229,7 +229,7 @@ void appends_created_columns_at_the_end() {
@Test @DisplayName("appends columns created from an id at the end")
@SuppressWarnings("unchecked")
void appends_columns_created_from_an_id_at_the_end() {
ColumnId<String> str = id(String.class, "str");
ColumnId<String> str = id("str", String.class);
people.columns().create(str, "a", "c", "b", "d");
Column<String> last = (Column<String>) people.columns().last();

Expand Down Expand Up @@ -287,20 +287,20 @@ void throws_when_asked_for_the_index_of_a_non_existing_header() {
@CsvSource({"'NAme', 'java.lang.String', 0", "'agE', 'java.lang.Integer', 1", "'sex', 'java.lang.String', 2"})
@DisplayName("can return the index of a column from its id")
void can_return_the_index_of_a_column_from_its_id(String header, String className, int expectedIndex) throws ClassNotFoundException {
assertThat(people.columns().indexOf(id(Class.forName(className), header)))
assertThat(people.columns().indexOf(id(header, Class.forName(className))))
.isEqualTo(expectedIndex);
}

@Test @DisplayName("throws when asked for the index of a column which id's type is wrong")
void throws_when_asked_for_the_index_of_an_id_with_wrong_type() {
assertThatExceptionOfType(ColumnIdNotFoundException.class)
.isThrownBy(() -> people.columns().indexOf(id(String.class, "age")));
.isThrownBy(() -> people.columns().indexOf(id("age", String.class)));
}

@Test @DisplayName("throws when asked for the index of a column which id's header is wrong")
void throws_when_asked_for_the_index_of_an_id_with_wrong_header() {
assertThatExceptionOfType(ColumnIdNotFoundException.class)
.isThrownBy(() -> people.columns().indexOf(id(Integer.class, "name")));
.isThrownBy(() -> people.columns().indexOf(id("name", Integer.class)));
}

// iterator()
Expand Down Expand Up @@ -369,7 +369,7 @@ void leaves_empty_rows_on_clear() {

@Test @DisplayName("throws when getting a column from an id with non existing header")
void throws_when_getting_a_column_from_an_id_with_non_existing_header() {
ColumnId<String> nonExisting = id(String.class, "string");
ColumnId<String> nonExisting = id("string", String.class);
assertThatExceptionOfType(ColumnIdNotFoundException.class)
.isThrownBy(() -> people.columns().get(nonExisting));
}
Expand All @@ -394,7 +394,7 @@ void throws_when_asked_for_a_column_with_index_gt_0() {

@Test @DisplayName("can return a column from its id")
void can_return_a_column_from_its_id() {
ColumnId<Integer> age = id(Integer.class, AGE_HEADER);
ColumnId<Integer> age = id(AGE_HEADER, Integer.class);
assertThat(people.columns().get(age)).containsExactly(23, 32, 0, 21);
}

Expand Down Expand Up @@ -439,7 +439,7 @@ void knows_when_it_contains_a_column_with_a_specific_id() {

@Test @DisplayName("knows when it does not contain a column with a specific id")
void knows_when_it_does_not_contain_any_column_with_a_specific_id() {
for( ColumnId<?> id : asList(id(Integer.class, NAME_HEADER), id(String.class, AGE_HEADER)) )
for( ColumnId<?> id : asList(id(NAME_HEADER, Integer.class), id(AGE_HEADER, String.class)) )
assertThat(people.columns().contains(id)).isFalse();
}
}
Expand Down
12 changes: 6 additions & 6 deletions src/test/java/fr/kazejiyu/generic/datatable/SimpleRowTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -156,23 +156,23 @@ void throws_when_asked_for_an_element_at_non_existing_header() {

@Test @DisplayName("throws when asked for an element at id with wrong header")
void throws_when_asked_for_an_element_at_id_with_wrong_header() {
ColumnId<String> nonExisting = id(String.class, "nonExisting");
ColumnId<String> nonExisting = id("nonExisting", String.class);

assertThatExceptionOfType(ColumnIdNotFoundException.class)
.isThrownBy(() -> people.rows().first().get(nonExisting));
}

@Test @DisplayName("throws when asked for an element at id with wrong type")
void throws_when_asked_for_an_element_at_id_with_wrong_type() {
ColumnId<Integer> nonExisting = id(Integer.class, NAME_HEADER);
ColumnId<Integer> nonExisting = id(NAME_HEADER, Integer.class);

assertThatExceptionOfType(ColumnIdNotFoundException.class)
.isThrownBy(() -> people.rows().first().get(nonExisting));
}

@Test @DisplayName("returns an element from its column's id")
void returns_an_element_from_its_column_id() {
ColumnId<String> name = id(String.class, NAME_HEADER);
ColumnId<String> name = id(NAME_HEADER, String.class);
assertThat(people.rows().first().get(name)).isEqualTo("Luc");
}

Expand Down Expand Up @@ -208,15 +208,15 @@ void throws_when_asked_to_set_an_element_at_non_existing_header() {

@Test @DisplayName("throws when asked to_set an element at id at wrong header")
void throws_when_asked_to_set_an_element_at_id_at_wrong_header() {
ColumnId<String> nonExisting = id(String.class, "nonExisting");
ColumnId<String> nonExisting = id("nonExisting", String.class);

assertThatExceptionOfType(ColumnIdNotFoundException.class)
.isThrownBy(() -> people.rows().first().set(nonExisting, null));
}

@Test @DisplayName("throws when asked to_set an element at id at wrong type")
void throws_when_asked_to_set_an_element_at_id_at_wrong_type() {
ColumnId<Integer> nonExisting = id(Integer.class, NAME_HEADER);
ColumnId<Integer> nonExisting = id(NAME_HEADER, Integer.class);

assertThatExceptionOfType(ColumnIdNotFoundException.class)
.isThrownBy(() -> people.rows().first().set(nonExisting, null));
Expand All @@ -236,7 +236,7 @@ void throws_when_asked_to_set_at_index_an_element_of_unexpected_type() {

@Test @DisplayName("can set an element from its column's id")
void can_set_an_element_from_its_column_id() {
ColumnId<String> name = id(String.class, NAME_HEADER);
ColumnId<String> name = id(NAME_HEADER, String.class);
people.rows().first().set(name, "Gandalf");

assertThat(people.rows().first().get(name)).isEqualTo("Gandalf");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@ class NonEmpty {
private static final String NAME_HEADER = "name";
private static final String SEX_HEADER = "sEx";

private final ColumnId<String> NAME = id(String.class, NAME_HEADER);
private final ColumnId<Integer> AGE = id(Integer.class, AGE_HEADER);
private final ColumnId<String> SEX = id(String.class, SEX_HEADER);
private final ColumnId<String> NAME = id(NAME_HEADER, String.class);
private final ColumnId<Integer> AGE = id(AGE_HEADER, Integer.class);
private final ColumnId<String> SEX = id(SEX_HEADER, String.class);

@BeforeEach
void initializePeopleTable() {
Expand Down Expand Up @@ -143,7 +143,7 @@ void can_apply_a_filter_on_an_array_of_column_of_strings_ids() {
@Test @DisplayName("can apply a filter on an array of ColumnOfNumbersIds")
void can_apply_a_filter_on_an_array_of_column_of_numbers_ids() {
people.columns().create("Size", Integer.class, 122, 0, 42, -45);
ColumnId<Integer> SIZE = id(Integer.class, "Size");
ColumnId<Integer> SIZE = id("Size", Integer.class);

Table result = Query
.from(people)
Expand Down
Loading

0 comments on commit 8129192

Please sign in to comment.