Skip to content

Commit

Permalink
Merge pull request #10 from mmnaseri/release-v1.1.0
Browse files Browse the repository at this point in the history
Release v1.1.0
  • Loading branch information
mmnaseri committed May 9, 2020
2 parents df2d14f + 3228762 commit 892aced
Show file tree
Hide file tree
Showing 17 changed files with 916 additions and 69 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -133,11 +133,7 @@ default <X extends Z> LabeledTuple<Z> extend(Supplier<X> value, String label) {
/**
* Returns a map from the labels to the values of this tuple.
*/
default FluentMap<String, Z> asMap() {
return IntStream.range(0, size())
.boxed()
.collect(FluentMap::new, (map, index) -> map.put(label(index), get(index)), Map::putAll);
}
FluentMap<String, Z> asMap();

@SuppressWarnings("unchecked")
@Override
Expand Down
10 changes: 8 additions & 2 deletions tuples4j/src/main/java/com/mmnaseri/utils/tuples/Tuple.java
Original file line number Diff line number Diff line change
Expand Up @@ -233,14 +233,20 @@ default TwelveTuple<Object, Z, Z, Z, Z, Z, Z, Z, Z, Z, Z, Z, Z> asTwelve() {
}

/**
* Tightens the current tuple's super-type. If the tightened type is not applicable to all elements,
* this would lead to an eventual {@link ClassCastException} when trying to access those elements.
* Tightens the current tuple's super-type. If the tightened type is not applicable to all elements, this would lead to an eventual {@link ClassCastException} when trying to access those elements.
*/
@SuppressWarnings("unchecked")
default <X extends Z> Tuple<X> tighten() {
return (Tuple<X>) this;
}

/**
* Tightens the current tuple's super-type. If the tightened type is not applicable to all elements, this would lead to an eventual {@link ClassCastException} when trying to access those elements.
*/
default <X extends Z> Tuple<X> tighten(Class<X> type) {
return tighten();
}

// Static utility and builder methods.

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,16 +68,10 @@ public Tuple<Z> drop(int index) {
return ((HasTenth<Z, ?, ?>) this).dropTenth();
case 10:
return ((HasEleventh<Z, ?, ?>) this).dropEleventh();
case 11:
return ((HasTwelfth<Z, ?, ?>) this).dropTwelfth();
default:
return dropAtIndex(index);
return ((HasTwelfth<Z, ?, ?>) this).dropTwelfth();
}
}

protected Tuple<Z> dropAtIndex(int index) {
throw new UnsupportedOperationException();
}

}

Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package com.mmnaseri.utils.tuples.impl;

import com.mmnaseri.utils.tuples.LabeledTuple;
import com.mmnaseri.utils.tuples.utils.FluentList;
import com.mmnaseri.utils.tuples.utils.FluentMap;

import java.util.List;
import java.util.function.Supplier;
import java.util.stream.Stream;

public class DelegatingLabeledTuple<Z> implements LabeledTuple<Z> {

private final LabeledTuple<Z> delegate;

public DelegatingLabeledTuple(final LabeledTuple<Z> delegate) {
this.delegate = delegate;
}

@Override
public List<String> labels() {
return delegate.labels();
}

@Override
public int size() {
return delegate.size();
}

@Override
public Z get(final int index) {
return delegate.get(index);
}

@Override
public LabeledTuple<Z> change(final int index, final Supplier<? extends Z> value) {
return delegate.change(index, value);
}

@Override
public LabeledTuple<Z> drop(final int index) {
return delegate.drop(index);
}

@Override
public LabeledTuple<Z> relabel(final int index, final String newLabel) {
return delegate.relabel(index, newLabel);
}

@Override
public <X extends Z> LabeledTuple<Z> extend(final X value) {
return delegate.extend(value);
}

@Override
public <X extends Z> LabeledTuple<Z> extend(final Supplier<X> value) {
return delegate.extend(value);
}

@Override
public FluentMap<String, Z> asMap() {
return delegate.asMap();
}

@Override
public Stream<Z> stream() {
return delegate.stream();
}

@Override
public FluentList<Z> asList() {
return delegate.asList();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package com.mmnaseri.utils.tuples.impl;

import com.mmnaseri.utils.tuples.Tuple;
import com.mmnaseri.utils.tuples.utils.FluentList;

import java.util.function.Supplier;
import java.util.stream.Stream;

public class DelegatingTuple<Z> implements Tuple<Z> {

private final Tuple<Z> delegate;

public DelegatingTuple(Tuple<Z> delegate) {
this.delegate = delegate;
}

@Override
public int size() {
return delegate.size();
}

@Override
public Z get(final int index) {
return delegate.get(index);
}

@Override
public Tuple<Z> change(final int index, final Supplier<? extends Z> value) {
return delegate.change(index, value);
}

@Override
public Tuple<Z> drop(final int index) {
return delegate.drop(index);
}

@Override
public <X extends Z> Tuple<Z> extend(final Supplier<X> value) {
return delegate.extend(value);
}

@Override
public Stream<Z> stream() {
return delegate.stream();
}

@Override
public FluentList<Z> asList() {
return delegate.asList();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,15 @@ public class EmptyTuple<Z> extends AbstractFixedTuple<Z, EmptyTuple<Z>> {
*/
@Override
public EmptyTuple<Z> change(int index, Supplier<? extends Z> supplier) {
checkIndex(index, size());
return new EmptyTuple<>();
return checkIndex(index, size());
}

/**
* Returns a new tuple by keeping all the values from this tuple and overriding the value at the provided index with the value returned from the function.
*/
@Override
public EmptyTuple<Z> change(int index, Function<EmptyTuple<Z>, ? extends Z> function) {
checkIndex(index, size());
return new EmptyTuple<>();
return checkIndex(index, size());
}

/**
Expand Down
Loading

0 comments on commit 892aced

Please sign in to comment.