com.github.underscore.math.$ Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of underscore-math Show documentation
Show all versions of underscore-math Show documentation
The math plugin for underscore-java
/*
* The MIT License (MIT)
*
* Copyright 2015 Valentyn Kolesnikov
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package com.github.underscore.math;
import java.util.*;
public class $ extends com.github.underscore.$ {
public $(final Iterable iterable) {
super(iterable);
}
public $(final String string) {
super(string);
}
public static class Chain extends com.github.underscore.$.Chain {
public Chain(final T item) {
super(item);
}
public Chain(final List list) {
super(list);
}
public Chain sum() {
return new Chain($.sum((List) value()));
}
public Chain mean() {
return new Chain($.mean((List) value()));
}
public Chain median() {
return new Chain($.median((List) value()));
}
}
public static Chain chain(final List list) {
return new $.Chain(list);
}
public static Chain chain(final Iterable iterable) {
return new $.Chain(newArrayList(iterable));
}
public static Chain chain(final T[] list) {
return new $.Chain(Arrays.asList(list));
}
public Chain chain() {
return new $.Chain(newArrayList(value()));
}
public static T sum(final Iterable iterable) {
T result = null;
for (final T item : iterable) {
result = sum(result, item);
}
return result;
}
public T sum() {
return (T) sum((List) getIterable());
}
private static T sum(final T first, final T second) {
if (first == null) {
return second;
} else if (second == null) {
return first;
} else if (first instanceof java.math.BigDecimal) {
return (T) sum((java.math.BigDecimal) first, (java.math.BigDecimal) second);
} else if (second instanceof java.math.BigInteger) {
return (T) sum((java.math.BigInteger) first, (java.math.BigInteger) second);
} else if (first instanceof Byte) {
return (T) sum((Byte) first, (Byte) second);
} else if (first instanceof Double) {
return (T) sum((Double) first, (Double) second);
} else if (first instanceof Float) {
return (T) sum((Float) first, (Float) second);
} else if (first instanceof Integer) {
return (T) sum((Integer) first, (Integer) second);
} else if (first instanceof Long) {
return (T) sum((Long) first, (Long) second);
} else if (first instanceof Short) {
return (T) sum((Short) first, (Short) second);
} else {
throw new UnsupportedOperationException("Sum only supports official subclasses of Number");
}
}
private static java.math.BigDecimal sum(java.math.BigDecimal first, java.math.BigDecimal second) {
return first.add(second);
}
private static java.math.BigInteger sum(java.math.BigInteger first, java.math.BigInteger second) {
return first.add(second);
}
private static Byte sum(Byte first, Byte second) {
return (byte) (first + second);
}
private static Double sum(Double first, Double second) {
return first + second;
}
private static Float sum(Float first, Float second) {
return first + second;
}
private static Integer sum(Integer first, Integer second) {
return first + second;
}
private static Long sum(Long first, Long second) {
return first + second;
}
private static Short sum(Short first, Short second) {
return (short) (first + second);
}
public static double mean(final Iterable iterable) {
T result = null;
int count = 0;
for (final T item : iterable) {
result = sum(result, item);
count += 1;
}
return result.doubleValue() / count;
}
public double mean() {
return mean((Iterable) getIterable());
}
public static double median(final Iterable iterable) {
final List result = newArrayList((Collection) iterable);
final int size = size(iterable);
if (size % 2 != 0) {
return result.get(size / 2).doubleValue();
}
return (result.get(size / 2 - 1).doubleValue() + result.get(size / 2).doubleValue()) / 2;
}
public double median() {
return median((Iterable) getIterable());
}
public static void main(String[] args) {
final String message = "Underscore-java-math is a math plugin for underscore-java.\n\n"
+ "For docs, license, tests, and downloads, see: http://javadev.github.io/underscore-java";
System.out.println(message);
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy