Skip to content

Commit 6c13594

Browse files
committed
- add average #20
1 parent 2e8b5f3 commit 6c13594

File tree

3 files changed

+153
-0
lines changed

3 files changed

+153
-0
lines changed

src/main/java/br/com/zbra/androidlinq/AbstractStream.java

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import br.com.zbra.androidlinq.exception.MultipleElementsFoundException;
66

77
import java.math.BigDecimal;
8+
import java.math.MathContext;
89
import java.util.*;
910

1011
abstract class AbstractStream<T> implements Stream<T> {
@@ -178,6 +179,47 @@ public BigDecimal aggregate(BigDecimal v, T t) {
178179
});
179180
}
180181

182+
@Override
183+
public Byte average(final SelectorByte<T> selector) {
184+
return (byte)(sum(selector) / count());
185+
}
186+
187+
@Override
188+
public Short average(SelectorShort<T> selector) {
189+
return (short)(sum(selector) / count());
190+
}
191+
192+
@Override
193+
public Integer average(SelectorInteger<T> selector) {
194+
return sum(selector) / count();
195+
}
196+
197+
@Override
198+
public Long average(SelectorLong<T> selector) {
199+
return sum(selector) / count();
200+
}
201+
202+
@Override
203+
public Float average(SelectorFloat<T> selector) {
204+
return sum(selector) / count();
205+
}
206+
207+
@Override
208+
public Double average(SelectorDouble<T> selector) {
209+
return sum(selector) / count();
210+
}
211+
212+
@Override
213+
public BigDecimal average(SelectorBigDecimal<T> selector) {
214+
//noinspection BigDecimalMethodWithoutRoundingCalled
215+
return sum(selector).divide(new BigDecimal(count()));
216+
}
217+
218+
@Override
219+
public BigDecimal average(SelectorBigDecimal<T> selector, MathContext mathContext) {
220+
return sum(selector).divide(new BigDecimal(count()), mathContext);
221+
}
222+
181223
@Override
182224
public <TResult extends Comparable<TResult>> T min(Selector<T, TResult> selector) {
183225
return orderBy(selector).first();

src/main/java/br/com/zbra/androidlinq/Stream.java

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import br.com.zbra.androidlinq.exception.MultipleElementsFoundException;
55

66
import java.math.BigDecimal;
7+
import java.math.MathContext;
78
import java.util.List;
89
import java.util.Map;
910
import java.util.NoSuchElementException;
@@ -203,6 +204,81 @@ interface Stream<T> extends Iterable<T> {
203204
*/
204205
BigDecimal sum(SelectorBigDecimal<T> selector);
205206

207+
/**
208+
* Computes the average of the sequence of Byte values that are obtained by invoking
209+
* a transform function on each element of the input sequence.
210+
*
211+
* @param selector A transform function to apply to each element.
212+
* @return The average of the projected values.
213+
*/
214+
Byte average(SelectorByte<T> selector);
215+
216+
/**
217+
* Computes the average of the sequence of Short values that are obtained by invoking
218+
* a transform function on each element of the input sequence.
219+
*
220+
* @param selector A transform function to apply to each element.
221+
* @return The average of the projected values.
222+
*/
223+
Short average(SelectorShort<T> selector);
224+
225+
/**
226+
* Computes the average of the sequence of Integer values that are obtained by invoking
227+
* a transform function on each element of the input sequence.
228+
*
229+
* @param selector A transform function to apply to each element.
230+
* @return The average of the projected values.
231+
*/
232+
Integer average(SelectorInteger<T> selector);
233+
234+
/**
235+
* Computes the average of the sequence of Long values that are obtained by invoking
236+
* a transform function on each element of the input sequence.
237+
*
238+
* @param selector A transform function to apply to each element.
239+
* @return The average of the projected values.
240+
*/
241+
Long average(SelectorLong<T> selector);
242+
243+
/**
244+
* Computes the average of the sequence of Float values that are obtained by invoking
245+
* a transform function on each element of the input sequence.
246+
*
247+
* @param selector A transform function to apply to each element.
248+
* @return The average of the projected values.
249+
*/
250+
Float average(SelectorFloat<T> selector);
251+
252+
/**
253+
* Computes the average of the sequence of Double values that are obtained by invoking
254+
* a transform function on each element of the input sequence.
255+
*
256+
* @param selector A transform function to apply to each element.
257+
* @return The average of the projected values.
258+
*/
259+
Double average(SelectorDouble<T> selector);
260+
261+
/**
262+
* Computes the average of the sequence of BigDecimal values that are obtained by invoking
263+
* a transform function on each element of the input sequence.
264+
*
265+
* @param selector A transform function to apply to each element.
266+
* @return The average of the projected values.
267+
*/
268+
BigDecimal average(SelectorBigDecimal<T> selector);
269+
270+
/**
271+
* Computes the average of the sequence of BigDecimal values that are obtained by invoking
272+
* a transform function on each element of the input sequence.
273+
*
274+
* @param selector A transform function to apply to each element.
275+
* @param mathContext Math context for operating with theses BigDecimals
276+
* @return The average of the projected values.
277+
* @see BigDecimal#divide(BigDecimal, MathContext)
278+
* @see MathContext
279+
*/
280+
BigDecimal average(SelectorBigDecimal<T> selector, MathContext mathContext);
281+
206282
/**
207283
* Computes the min of the sequence of values that are obtained by invoking
208284
* a transform function on each element of the input sequence.

src/test/groovy/br/com/zbra/androidlinq/StreamTest.groovy

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@ package br.com.zbra.androidlinq
33
import br.com.zbra.androidlinq.delegate.*
44
import br.com.zbra.androidlinq.exception.MultipleElementsFoundException
55

6+
import java.math.MathContext
7+
import java.math.RoundingMode
8+
69
import static br.com.zbra.androidlinq.Linq.stream as stream
710

811
@SuppressWarnings("GroovyUnusedDeclaration")
@@ -300,6 +303,38 @@ class StreamTest extends GroovyTestCase {
300303
assert sum == stream(list).sum({ int n -> new BigDecimal(n) } as SelectorBigDecimal)
301304
}
302305

306+
void testAverage() {
307+
def list = 0..9 as List<Integer>
308+
309+
// average Bytes
310+
assert 4 == stream(list).average({ n -> (byte) n } as SelectorByte)
311+
312+
// average Shorts
313+
assert 4 == stream(list).average({ n -> (short) n } as SelectorShort)
314+
315+
// average Integers
316+
assert 4 == stream(list).average({ n -> (int) n } as SelectorInteger)
317+
318+
// average Longs
319+
assert 4 == stream(list).average({ n -> (long) n } as SelectorLong)
320+
321+
// average Floats
322+
assert 4.5 == stream(list).average({ n -> (float) n } as SelectorFloat)
323+
324+
// average Doubles
325+
assert 4.5 == stream(list).average({ n -> (double) n } as SelectorDouble)
326+
327+
// average BigDecimals
328+
assert new BigDecimal(4.5) == stream(list).average({ int n -> new BigDecimal(n) } as SelectorBigDecimal)
329+
330+
// average BigDecimals with MathContext
331+
assert new BigDecimal(4.5) == stream(list).average({ int n -> new BigDecimal(n) } as SelectorBigDecimal, new MathContext(2, RoundingMode.HALF_UP))
332+
333+
// average BigDecimals with MathContext for recurring division
334+
assert new BigDecimal(0.33, new MathContext(2, RoundingMode.UP)) == stream([0, 0, 1]).average({ int n -> new BigDecimal(n) } as SelectorBigDecimal, new MathContext(2, RoundingMode.UP))
335+
336+
}
337+
303338
void testMax() {
304339

305340
// max for numbers

0 commit comments

Comments
 (0)