Skip to content
This repository has been archived by the owner on Aug 9, 2024. It is now read-only.

Commit

Permalink
Code refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
Laifsyn committed Apr 24, 2024
1 parent 53b8623 commit 6135095
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.utp.clsEstructuraDatos.pry3.Collections;
package com.utp.clsEstructuraDatos.pry3.utils.Collections;

public interface Tuple {
static <T1> Tuple1<T1> of(T1 t1) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.utp.clsEstructuraDatos.pry3.Collections;
package com.utp.clsEstructuraDatos.pry3.utils.Collections;

public class Tuple1<T1> implements Tuple {
public final T1 _1;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.utp.clsEstructuraDatos.pry3.Collections;
package com.utp.clsEstructuraDatos.pry3.utils.Collections;

public class Tuple2<T1, T2> implements Tuple {

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.utp.clsEstructuraDatos.pry3.Collections;
package com.utp.clsEstructuraDatos.pry3.utils.Collections;

public class Tuple3<T1, T2, T3> implements Tuple {

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.utp.clsEstructuraDatos.pry3.Collections;
package com.utp.clsEstructuraDatos.pry3.utils.Collections;

public class Tuple4<T1, T2, T3, T4> implements Tuple {

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.utp.clsEstructuraDatos.pry3.Collections;
package com.utp.clsEstructuraDatos.pry3.utils.Collections;

public class Tuple5<T1, T2, T3, T4, T5> implements Tuple {

Expand Down
47 changes: 47 additions & 0 deletions src/main/java/com/utp/clsEstructuraDatos/pry3/utils/Either.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package com.utp.clsEstructuraDatos.pry3.utils;

import java.util.Optional;

public class Either<T, E> {

private Optional<T> left;
private Optional<E> right;

private Either(T value, E error) {
this.left = Optional.ofNullable(value);
this.right = Optional.ofNullable(error);
}

public static <U, E> Either<U, E> left(U value) {
return new Either<>(value, null);
}

public static <U, E> Either<U, E> right(E error) {
return new Either<>(null, error);
}

public boolean isRight() {
return right.isPresent();
}

public boolean isLeft() {
return left.isPresent();
}

public T get() throws RuntimeException {
return unwrapLeft();
}

public T unwrapLeft() throws RuntimeException {
if (this.isRight())
throw new RuntimeException("Called unwrapLeft on an Right Either");
return left.get();
}

public E unwrapRight() throws RuntimeException {
if (this.isLeft())
throw new RuntimeException("Called UnwrapRight on an Left Either");
return right.get();
}

}

0 comments on commit 6135095

Please sign in to comment.