-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
NumberType and Range improvements, wrap, clip, factor
- Loading branch information
Showing
9 changed files
with
2,126 additions
and
1,620 deletions.
There are no files selected for viewing
1,891 changes: 1,026 additions & 865 deletions
1,891
core/src/main/java/io/github/mmm/base/number/NumberType.java
Large diffs are not rendered by default.
Oops, something went wrong.
248 changes: 130 additions & 118 deletions
248
core/src/main/java/io/github/mmm/base/range/AbstractRange.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,118 +1,130 @@ | ||
/* Copyright (c) The m-m-m Team, Licensed under the Apache License, Version 2.0 | ||
* http://www.apache.org/licenses/LICENSE-2.0 */ | ||
package io.github.mmm.base.range; | ||
|
||
import java.math.BigDecimal; | ||
import java.util.Comparator; | ||
import java.util.Objects; | ||
|
||
/** | ||
* Implementation of {@link Range}. | ||
* | ||
* @param <V> type of the contained values. | ||
* @since 1.0.0 | ||
*/ | ||
public abstract class AbstractRange<V extends Comparable<?>> implements Range<V> { | ||
|
||
@Override | ||
public Range<V> intersection(Range<V> range) { | ||
|
||
if ((range == null) || (range == RangeType.UNBOUNDED)) { | ||
return this; | ||
} else if (range == RangeType.INVALID) { | ||
return range; | ||
} | ||
Comparator<? super V> comparator = getComparator(); | ||
Range<V> result = this; | ||
V min1 = getMin(); | ||
V min2 = range.getMin(); | ||
V min = min1; | ||
if (min == null) { | ||
min = min2; | ||
} else if (min2 != null) { | ||
int delta = comparator.compare(min, min2); | ||
if (delta <= 0) { // min > min2? | ||
min = min2; | ||
} | ||
} | ||
V max1 = getMax(); | ||
V max2 = range.getMax(); | ||
V max = max1; | ||
if (max == null) { | ||
max = max2; | ||
} else if (max2 != null) { | ||
int delta = comparator.compare(max, max2); | ||
if (delta >= 0) { // max < max2? | ||
max = max2; | ||
} | ||
} | ||
if (((min != min1) || (max != max1)) && (min != null) && (max != null)) { | ||
int delta = comparator.compare(min, max); | ||
if (delta > 0) { | ||
return Range.invalid(); | ||
} | ||
} | ||
if ((min == min2) && (max == max2)) { | ||
return range; | ||
} | ||
if (min != min1) { | ||
result = result.withMin(min); | ||
} | ||
if (max != max1) { | ||
result = result.withMax(max); | ||
} | ||
return result; | ||
} | ||
|
||
@Override | ||
public boolean equals(Object obj) { | ||
|
||
if (obj == this) { | ||
return true; | ||
} | ||
if ((obj == null) || !(obj instanceof AbstractRange)) { | ||
return false; | ||
} | ||
AbstractRange<?> other = (AbstractRange<?>) obj; | ||
return Objects.equals(getMin(), other.getMin()) && Objects.equals(getMax(), other.getMax()); | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
|
||
return Objects.hash(getMin(), getMax()); | ||
} | ||
|
||
private String toString(V value) { | ||
|
||
if (value instanceof BigDecimal) { | ||
return ((BigDecimal) value).stripTrailingZeros().toPlainString(); | ||
} | ||
return value.toString(); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
|
||
StringBuilder buffer = new StringBuilder(); | ||
V min = getMin(); | ||
if (min == null) { | ||
buffer.append(BOUND_START_EXCLUSIVE); | ||
buffer.append(MIN_UNBOUND); | ||
} else { | ||
buffer.append(BOUND_START_INCLUSIVE); | ||
buffer.append(toString(min)); | ||
} | ||
V max = getMax(); | ||
buffer.append(BOUND_SEPARATOR); | ||
if (max == null) { | ||
buffer.append(MAX_UNBOUND); | ||
buffer.append(BOUND_END_EXCLUSIVE); | ||
} else { | ||
buffer.append(toString(max)); | ||
buffer.append(BOUND_END_INCLUSIVE); | ||
} | ||
return buffer.toString(); | ||
} | ||
|
||
} | ||
/* Copyright (c) The m-m-m Team, Licensed under the Apache License, Version 2.0 | ||
* http://www.apache.org/licenses/LICENSE-2.0 */ | ||
package io.github.mmm.base.range; | ||
|
||
import java.math.BigDecimal; | ||
import java.util.Comparator; | ||
import java.util.Objects; | ||
|
||
/** | ||
* Implementation of {@link Range}. | ||
* | ||
* @param <V> type of the contained values. | ||
* @since 1.0.0 | ||
*/ | ||
public abstract class AbstractRange<V extends Comparable<?>> implements Range<V> { | ||
|
||
@Override | ||
public Range<V> intersection(Range<V> range) { | ||
|
||
if ((range == null) || (range == RangeType.UNBOUNDED)) { | ||
return this; | ||
} else if (range == RangeType.INVALID) { | ||
return range; | ||
} | ||
Comparator<? super V> comparator = getComparator(); | ||
V min1 = getMin(); | ||
V min2 = range.getMin(); | ||
V min = min1; | ||
if (min == null) { | ||
min = min2; | ||
} else if (min2 != null) { | ||
int delta = comparator.compare(min, min2); | ||
if (delta <= 0) { // min > min2? | ||
min = min2; | ||
} | ||
} | ||
V max1 = getMax(); | ||
V max2 = range.getMax(); | ||
V max = max1; | ||
if (max == null) { | ||
max = max2; | ||
} else if (max2 != null) { | ||
int delta = comparator.compare(max, max2); | ||
if (delta >= 0) { // max < max2? | ||
max = max2; | ||
} | ||
} | ||
if (((min != min1) || (max != max1)) && (min != null) && (max != null)) { | ||
int delta = comparator.compare(min, max); | ||
if (delta > 0) { | ||
return Range.invalid(); | ||
} | ||
} | ||
if ((min == min2) && (max == max2)) { | ||
return range; | ||
} else if ((min == min1) && (max == max1)) { | ||
return this; | ||
} | ||
return with(min, max); | ||
} | ||
|
||
@Override | ||
public Range<V> with(V minimum, V maximum) { | ||
|
||
if (Objects.equals(minimum, getMin()) && Objects.equals(maximum, getMax())) { | ||
return this; | ||
} | ||
return create(minimum, maximum); | ||
} | ||
|
||
/** | ||
* @param minimum the new {@link #getMin() minimum}. | ||
* @param maximum the new {@link #getMax() maximum}. | ||
* @return a new {@link Range} with the given boundaries. | ||
* @see #with(Comparable, Comparable) | ||
*/ | ||
protected abstract Range<V> create(V minimum, V maximum); | ||
|
||
@Override | ||
public boolean equals(Object obj) { | ||
|
||
if (obj == this) { | ||
return true; | ||
} | ||
if ((obj == null) || !(obj instanceof AbstractRange)) { | ||
return false; | ||
} | ||
AbstractRange<?> other = (AbstractRange<?>) obj; | ||
return Objects.equals(getMin(), other.getMin()) && Objects.equals(getMax(), other.getMax()); | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
|
||
return Objects.hash(getMin(), getMax()); | ||
} | ||
|
||
private String toString(V value) { | ||
|
||
if (value instanceof BigDecimal) { | ||
return ((BigDecimal) value).stripTrailingZeros().toPlainString(); | ||
} | ||
return value.toString(); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
|
||
StringBuilder buffer = new StringBuilder(); | ||
V min = getMin(); | ||
if (min == null) { | ||
buffer.append(BOUND_START_EXCLUSIVE); | ||
buffer.append(MIN_UNBOUND); | ||
} else { | ||
buffer.append(BOUND_START_INCLUSIVE); | ||
buffer.append(toString(min)); | ||
} | ||
V max = getMax(); | ||
buffer.append(BOUND_SEPARATOR); | ||
if (max == null) { | ||
buffer.append(MAX_UNBOUND); | ||
buffer.append(BOUND_END_EXCLUSIVE); | ||
} else { | ||
buffer.append(toString(max)); | ||
buffer.append(BOUND_END_INCLUSIVE); | ||
} | ||
return buffer.toString(); | ||
} | ||
|
||
} |
93 changes: 93 additions & 0 deletions
93
core/src/main/java/io/github/mmm/base/range/AbstractRangeBean.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
/* Copyright (c) The m-m-m Team, Licensed under the Apache License, Version 2.0 | ||
* http://www.apache.org/licenses/LICENSE-2.0 */ | ||
package io.github.mmm.base.range; | ||
|
||
import java.util.Objects; | ||
|
||
/** | ||
* Implementation of {@link WritableRange} for {@code UiNumericInput}. | ||
* | ||
* @param <V> type of the contained values. | ||
* @since 1.0.0 | ||
*/ | ||
public abstract class AbstractRangeBean<V extends Comparable<?>> extends AbstractRange<V> implements WritableRange<V> { | ||
|
||
private V min; | ||
|
||
private V max; | ||
|
||
/** | ||
* The constructor. | ||
*/ | ||
public AbstractRangeBean() { | ||
|
||
super(); | ||
} | ||
|
||
/** | ||
* The constructor. | ||
* | ||
* @param min the {@link #getMin() minimum}. | ||
* @param max the {@link #getMax() maximum}. | ||
*/ | ||
public AbstractRangeBean(V min, V max) { | ||
|
||
super(); | ||
this.min = min; | ||
this.max = max; | ||
} | ||
|
||
@Override | ||
public V getMin() { | ||
|
||
return this.min; | ||
} | ||
|
||
@Override | ||
public void setMin(V min) { | ||
|
||
if (Objects.equals(min, this.min)) { | ||
return; | ||
} | ||
this.min = min; | ||
onValueChange(); | ||
} | ||
|
||
@Override | ||
public V getMax() { | ||
|
||
return this.max; | ||
} | ||
|
||
@Override | ||
public void setMax(V max) { | ||
|
||
if (Objects.equals(max, this.max)) { | ||
return; | ||
} | ||
this.max = max; | ||
onValueChange(); | ||
} | ||
|
||
/** | ||
* Called whenever {@link #getMin() min} or {@link #getMax() max} changes. | ||
*/ | ||
protected void onValueChange() { | ||
|
||
} | ||
|
||
@Override | ||
public final Range<V> with(V minimum, V maximum) { | ||
|
||
return create(minimum, maximum); | ||
} | ||
|
||
@Override | ||
protected final Range<V> create(V minimum, V maximum) { | ||
|
||
setMin(minimum); | ||
setMax(maximum); | ||
return this; | ||
} | ||
|
||
} |
Oops, something went wrong.