Skip to content

Commit

Permalink
Add util classes
Browse files Browse the repository at this point in the history
  • Loading branch information
nroduit committed Sep 12, 2024
1 parent 1959622 commit 28750f0
Show file tree
Hide file tree
Showing 6 changed files with 150 additions and 9 deletions.
12 changes: 12 additions & 0 deletions weasis-core-img/src/main/java/org/weasis/core/util/Pair.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/*
* Copyright (c) 2024 Weasis Team and other contributors.
*
* This program and the accompanying materials are made available under the terms of the Eclipse
* Public License 2.0 which is available at https://www.eclipse.org/legal/epl-2.0, or the Apache
* License, Version 2.0 which is available at https://www.apache.org/licenses/LICENSE-2.0.
*
* SPDX-License-Identifier: EPL-2.0 OR Apache-2.0
*/
package org.weasis.core.util;

public record Pair<K, V>(K first, V second) {}
Original file line number Diff line number Diff line change
Expand Up @@ -157,10 +157,6 @@ public static boolean hasLength(CharSequence str) {
return str != null && !str.isEmpty();
}

public static boolean hasLength(String str) {
return hasLength((CharSequence) str);
}

public static boolean hasText(CharSequence str) {
if (!hasLength(str)) {
return false;
Expand All @@ -174,10 +170,6 @@ public static boolean hasText(CharSequence str) {
return false;
}

public static boolean hasText(String str) {
return hasText((CharSequence) str);
}

/**
* Removing diacritical marks aka accents
*
Expand Down
12 changes: 12 additions & 0 deletions weasis-core-img/src/main/java/org/weasis/core/util/Triple.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/*
* Copyright (c) 2024 Weasis Team and other contributors.
*
* This program and the accompanying materials are made available under the terms of the Eclipse
* Public License 2.0 which is available at https://www.eclipse.org/legal/epl-2.0, or the Apache
* License, Version 2.0 which is available at https://www.apache.org/licenses/LICENSE-2.0.
*
* SPDX-License-Identifier: EPL-2.0 OR Apache-2.0
*/
package org.weasis.core.util;

public record Triple<K, V, T>(K first, V second, T third) {}
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import java.util.Properties;
import javax.imageio.stream.ImageInputStream;
import javax.imageio.stream.MemoryCacheImageInputStream;
Expand Down
58 changes: 58 additions & 0 deletions weasis-core-img/src/test/java/org/weasis/core/util/PairTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/*
* Copyright (c) 2024 Weasis Team and other contributors.
*
* This program and the accompanying materials are made available under the terms of the Eclipse
* Public License 2.0 which is available at https://www.eclipse.org/legal/epl-2.0, or the Apache
* License, Version 2.0 which is available at https://www.apache.org/licenses/LICENSE-2.0.
*
* SPDX-License-Identifier: EPL-2.0 OR Apache-2.0
*/
package org.weasis.core.util;

import static org.junit.jupiter.api.Assertions.*;

import org.junit.jupiter.api.Test;

class PairTest {
@Test
void pairStoresFirstAndSecondValues() {
Pair<String, Integer> pair = new Pair<>("first", 1);
assertEquals("first", pair.first());
assertEquals(1, pair.second());
}

@Test
void pairHandlesMixedNullAndNonNullValues() {
Pair<String, Integer> pair = new Pair<>("first", null);
assertEquals("first", pair.first());
assertNull(pair.second());

pair = new Pair<>(null, 1);
assertNull(pair.first());
assertEquals(1, pair.second());
}

@Test
void pairWithDifferentTypes() {
Pair<Integer, String> pair = new Pair<>(1, "second");
assertEquals(1, pair.first());
assertEquals("second", pair.second());
}

@Test
void pairEquality() {
Pair<String, Integer> pair1 = new Pair<>("first", 1);
Pair<String, Integer> pair2 = new Pair<>("first", 1);
assertEquals(pair1, pair2);
}

@Test
void pairInequality() {
Pair<String, Integer> pair1 = new Pair<>("first", 1);
Pair<String, Integer> pair2 = new Pair<>("second", 1);
assertNotEquals(pair1, pair2);

pair2 = new Pair<>("first", 2);
assertNotEquals(pair1, pair2);
}
}
68 changes: 68 additions & 0 deletions weasis-core-img/src/test/java/org/weasis/core/util/TripleTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/*
* Copyright (c) 2024 Weasis Team and other contributors.
*
* This program and the accompanying materials are made available under the terms of the Eclipse
* Public License 2.0 which is available at https://www.eclipse.org/legal/epl-2.0, or the Apache
* License, Version 2.0 which is available at https://www.apache.org/licenses/LICENSE-2.0.
*
* SPDX-License-Identifier: EPL-2.0 OR Apache-2.0
*/
package org.weasis.core.util;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotEquals;
import static org.junit.jupiter.api.Assertions.assertNull;

import org.junit.jupiter.api.Test;

class TripleTest {

@Test
void tripleStoresFirstSecondAndThirdValues() {
Triple<String, Integer, Double> triple = new Triple<>("first", 1, 2.0);
assertEquals("first", triple.first());
assertEquals(1, triple.second());
assertEquals(2.0, triple.third());
}

@Test
void tripleHandlesMixedNullAndNonNullValues() {
Triple<String, Integer, Double> triple = new Triple<>("first", null, 2.0);
assertEquals("first", triple.first());
assertNull(triple.second());
assertEquals(2.0, triple.third());

triple = new Triple<>(null, 1, null);
assertNull(triple.first());
assertEquals(1, triple.second());
assertNull(triple.third());
}

@Test
void tripleWithDifferentTypes() {
Triple<Integer, String, Boolean> triple = new Triple<>(1, "second", true);
assertEquals(1, triple.first());
assertEquals("second", triple.second());
assertEquals(true, triple.third());
}

@Test
void tripleEquality() {
Triple<String, Integer, Double> triple1 = new Triple<>("first", 1, 2.0);
Triple<String, Integer, Double> triple2 = new Triple<>("first", 1, 2.0);
assertEquals(triple1, triple2);
}

@Test
void tripleInequality() {
Triple<String, Integer, Double> triple1 = new Triple<>("first", 1, 2.0);
Triple<String, Integer, Double> triple2 = new Triple<>("second", 1, 2.0);
assertNotEquals(triple1, triple2);

triple2 = new Triple<>("first", 2, 2.0);
assertNotEquals(triple1, triple2);

triple2 = new Triple<>("first", 1, 3.0);
assertNotEquals(triple1, triple2);
}
}

0 comments on commit 28750f0

Please sign in to comment.