http://git-wip-us.apache.org/repos/asf/zest-java/blob/bd6fbad9/core/api/src/main/java/org/apache/zest/api/util/Fields.java ---------------------------------------------------------------------- diff --git a/core/api/src/main/java/org/apache/zest/api/util/Fields.java b/core/api/src/main/java/org/apache/zest/api/util/Fields.java index 9e5768c..43f796d 100644 --- a/core/api/src/main/java/org/apache/zest/api/util/Fields.java +++ b/core/api/src/main/java/org/apache/zest/api/util/Fields.java @@ -20,8 +20,10 @@ package org.apache.zest.api.util; import java.lang.reflect.Field; import java.lang.reflect.Type; +import java.util.Arrays; import java.util.function.BiFunction; import java.util.function.Function; +import java.util.stream.Stream; import org.apache.zest.functional.Iterables; import static org.apache.zest.functional.Iterables.iterable; @@ -31,21 +33,14 @@ import static org.apache.zest.functional.Iterables.iterable; */ public final class Fields { - public static final BiFunction<Class<?>, String, Field> FIELD_NAMED = new BiFunction<Class<?>, String, Field>() - { - @Override - public Field apply( Class<?> aClass, String name ) - { - return Iterables.first( Iterables.filter( Classes.memberNamed( name ), FIELDS_OF.apply( aClass ) ) ); - } - }; + public static final Function<Type, Stream<Field>> FIELDS_OF = + Classes.forClassHierarchy( type -> Arrays.stream( type.getDeclaredFields() ) ); + + public static final BiFunction<Class<?>, String, Field> FIELD_NAMED = ( clazz, name ) -> + FIELDS_OF.apply( clazz ).filter( Classes.memberNamed( name ) ).findFirst().orElse( null ); - public static final Function<Type, Iterable<Field>> FIELDS_OF = Classes.forClassHierarchy( new Function<Class<?>, Iterable<Field>>() + public static Stream<Field> fieldsOf( Type type ) { - @Override - public Iterable<Field> apply( Class<?> type ) - { - return iterable( type.getDeclaredFields() ); - } - } ); + return Stream.of( type ).flatMap( FIELDS_OF ); + } }
http://git-wip-us.apache.org/repos/asf/zest-java/blob/bd6fbad9/core/api/src/main/java/org/apache/zest/api/util/Methods.java ---------------------------------------------------------------------- diff --git a/core/api/src/main/java/org/apache/zest/api/util/Methods.java b/core/api/src/main/java/org/apache/zest/api/util/Methods.java index 98095b7..ad45c18 100644 --- a/core/api/src/main/java/org/apache/zest/api/util/Methods.java +++ b/core/api/src/main/java/org/apache/zest/api/util/Methods.java @@ -20,31 +20,25 @@ package org.apache.zest.api.util; import java.lang.reflect.Method; import java.lang.reflect.Type; +import java.util.Arrays; import java.util.function.Function; import java.util.function.Predicate; - -import static org.apache.zest.functional.Iterables.iterable; +import java.util.stream.Stream; /** * Useful methods for handling Methods. */ public class Methods { - public static final Predicate<Type> HAS_METHODS = new Predicate<Type>() - { - @Override - public boolean test( Type item ) - { - return Classes.RAW_CLASS.apply( item ).getDeclaredMethods().length > 0; - } - }; + public static final Predicate<Type> HAS_METHODS = + item -> Classes.RAW_CLASS.apply( item ).getDeclaredMethods().length > 0; + + public static final Function<Type, Stream<Method>> METHODS_OF = Classes.forTypes( type -> + Stream.of( type ).map( Classes.RAW_CLASS ).flatMap( clazz -> Arrays.stream( clazz.getDeclaredMethods() ) ) + ); - public static final Function<Type, Iterable<Method>> METHODS_OF = Classes.forTypes( new Function<Type, Iterable<Method>>() + public static Stream<Method> methodsOf( Type type ) { - @Override - public Iterable<Method> apply( Type type ) - { - return iterable( Classes.RAW_CLASS.apply( type ).getDeclaredMethods() ); - } - } ); + return Stream.of(type).flatMap( METHODS_OF ); + } } http://git-wip-us.apache.org/repos/asf/zest-java/blob/bd6fbad9/core/api/src/test/java/org/apache/zest/api/util/ClassesTest.java ---------------------------------------------------------------------- diff --git a/core/api/src/test/java/org/apache/zest/api/util/ClassesTest.java b/core/api/src/test/java/org/apache/zest/api/util/ClassesTest.java index 4a9321d..088a400 100644 --- a/core/api/src/test/java/org/apache/zest/api/util/ClassesTest.java +++ b/core/api/src/test/java/org/apache/zest/api/util/ClassesTest.java @@ -20,17 +20,13 @@ import java.lang.reflect.Type; import java.lang.reflect.TypeVariable; import java.util.HashSet; import java.util.Set; -import java.util.function.Predicate; import org.junit.Test; -import org.apache.zest.functional.Iterables; -import org.apache.zest.functional.Specifications; +import static org.apache.zest.api.util.Classes.interfacesOf; +import static org.apache.zest.api.util.Classes.interfacesWithMethods; import static org.hamcrest.CoreMatchers.equalTo; import static org.hamcrest.CoreMatchers.is; import static org.junit.Assert.assertThat; -import static org.apache.zest.api.util.Classes.interfacesOf; -import static org.apache.zest.api.util.Classes.interfacesWithMethods; -import static org.apache.zest.functional.Iterables.count; /** * Tests for Classes @@ -41,9 +37,9 @@ public class ClassesTest @Test public void givenClassWithInterfacesWhenInterfacesOfThenGetCorrectSet() { - assertThat( "one interface returned", count( interfacesOf( A.class ) ), equalTo( 1L ) ); - assertThat( "two interface returned", count( interfacesOf( B.class ) ), equalTo( 2L ) ); - assertThat( "tree interface returned", count( interfacesOf( C.class ) ), equalTo( 4L ) ); + assertThat( "one interface returned", interfacesOf( A.class ).count(), equalTo( 1L ) ); + assertThat( "two interface returned", interfacesOf( B.class ).count(), equalTo( 2L ) ); + assertThat( "tree interface returned", interfacesOf( C.class ).count(), equalTo( 4L ) ); } @Test @@ -59,10 +55,11 @@ public class ClassesTest @Test public void givenClassesWithInterfacesWhenGetInterfacesWithMethodsThenGetCorrectSet() { - Iterable<Type> types = Iterables.filter( Methods.HAS_METHODS, interfacesOf( C.class ) ); - assertThat( "one interface returned", count( types ), equalTo( 1L ) ); - assertThat( "correct interface returned", Iterables.matchesAny( (Predicate) Specifications.in( B.class ), Iterables - .<Class<?>>cast( types ) ), is( true ) ); + assertThat( "one interface returned", interfacesOf( C.class ).filter( Methods.HAS_METHODS ) + .count(), equalTo( 1L ) ); + boolean isIn = interfacesOf( C.class ).filter( Methods.HAS_METHODS ) + .anyMatch( B.class::equals ); + assertThat( "correct interface returned", isIn, is( true ) ); } @Test @@ -82,7 +79,7 @@ public class ClassesTest throws NoSuchMethodException { Type returnType = Generics.class.getMethod( "wildcard" ).getGenericReturnType(); - Type wildcardType = ( (ParameterizedType) returnType ).getActualTypeArguments()[ 0]; + Type wildcardType = ( (ParameterizedType) returnType ).getActualTypeArguments()[ 0 ]; assertThat( "Return type is A", Classes.RAW_CLASS.apply( wildcardType ), equalTo( (Class) A.class ) ); } @@ -97,15 +94,15 @@ public class ClassesTest System.out.println( type + "=" + resolvedType ); switch( method.getName() ) { - case "type": - assertThat( resolvedType, equalTo( (Type) String.class ) ); - break; - case "type1": - assertThat( resolvedType, equalTo( (Type) String.class ) ); - break; - case "type2": - assertThat( resolvedType, equalTo( (Type) Long.class ) ); - break; + case "type": + assertThat( resolvedType, equalTo( (Type) String.class ) ); + break; + case "type1": + assertThat( resolvedType, equalTo( (Type) String.class ) ); + break; + case "type2": + assertThat( resolvedType, equalTo( (Type) Long.class ) ); + break; } } } @@ -174,7 +171,6 @@ public class ClassesTest { public void doStuff(); - } interface C @@ -186,7 +182,6 @@ public class ClassesTest { Iterable<? extends A> wildcard(); - } interface Type1 @@ -201,14 +196,11 @@ public class ClassesTest TYPE1 type1(); TYPE2 type2(); - } interface Type3<TYPE> { TYPE type(); - } - } http://git-wip-us.apache.org/repos/asf/zest-java/blob/bd6fbad9/core/bootstrap/src/main/java/org/apache/zest/bootstrap/AssemblySpecifications.java ---------------------------------------------------------------------- diff --git a/core/bootstrap/src/main/java/org/apache/zest/bootstrap/AssemblySpecifications.java b/core/bootstrap/src/main/java/org/apache/zest/bootstrap/AssemblySpecifications.java index a17238b..2787deb 100644 --- a/core/bootstrap/src/main/java/org/apache/zest/bootstrap/AssemblySpecifications.java +++ b/core/bootstrap/src/main/java/org/apache/zest/bootstrap/AssemblySpecifications.java @@ -18,32 +18,17 @@ */ package org.apache.zest.bootstrap; +import java.util.Arrays; import java.util.function.Predicate; import org.apache.zest.api.type.HasTypes; -import org.apache.zest.functional.Specifications; /** * Utility specifications for Assemblies. */ public class AssemblySpecifications { - public static Predicate<HasTypes> types( final Class... types ) + public static Predicate<HasTypes> ofAnyType( final Class... types ) { - return new Predicate<HasTypes>() - { - @Override - public boolean test( HasTypes item ) - { - - for( Class<?> type : item.types() ) - { - if( Specifications.in( types ).test( type ) ) - { - return true; - } - } - return false; - } - }; + return item -> item.types().anyMatch( a -> Arrays.stream( types ).anyMatch( a::equals ) ); } } http://git-wip-us.apache.org/repos/asf/zest-java/blob/bd6fbad9/core/bootstrap/src/main/java/org/apache/zest/bootstrap/builder/ModuleDeclaration.java ---------------------------------------------------------------------- diff --git a/core/bootstrap/src/main/java/org/apache/zest/bootstrap/builder/ModuleDeclaration.java b/core/bootstrap/src/main/java/org/apache/zest/bootstrap/builder/ModuleDeclaration.java index 065e9d8..4582d7a 100644 --- a/core/bootstrap/src/main/java/org/apache/zest/bootstrap/builder/ModuleDeclaration.java +++ b/core/bootstrap/src/main/java/org/apache/zest/bootstrap/builder/ModuleDeclaration.java @@ -28,7 +28,6 @@ import org.apache.zest.bootstrap.ModuleAssembly; import static org.apache.zest.api.util.Classes.isAssignableFrom; import static org.apache.zest.functional.Iterables.filter; import static org.apache.zest.functional.Iterables.toList; -import static org.apache.zest.functional.Specifications.not; /** * Provides declared {@link org.apache.zest.api.structure.Module} information that the {@link ApplicationBuilder} can use. @@ -94,7 +93,7 @@ public class ModuleDeclaration throws AssemblyException { List<Class<?>> notAssemblers = toList( - filter( not( isAssignableFrom( Assembler.class ) ), + filter( isAssignableFrom( Assembler.class ).negate(), assemblerClasses ) ); if( !notAssemblers.isEmpty() ) http://git-wip-us.apache.org/repos/asf/zest-java/blob/bd6fbad9/core/bootstrap/src/test/java/org/apache/zest/bootstrap/DocumentationSupport.java ---------------------------------------------------------------------- diff --git a/core/bootstrap/src/test/java/org/apache/zest/bootstrap/DocumentationSupport.java b/core/bootstrap/src/test/java/org/apache/zest/bootstrap/DocumentationSupport.java index bd081d0..058f070 100644 --- a/core/bootstrap/src/test/java/org/apache/zest/bootstrap/DocumentationSupport.java +++ b/core/bootstrap/src/test/java/org/apache/zest/bootstrap/DocumentationSupport.java @@ -37,7 +37,7 @@ public class DocumentationSupport public boolean test( ObjectAssembly item ) { - return Iterables.toList( item.types() ).contains( String.class ); + return item.types().anyMatch( type -> type.equals(String.class) ); } }; http://git-wip-us.apache.org/repos/asf/zest-java/blob/bd6fbad9/core/functional/src/main/java/org/apache/zest/functional/ForEach.java ---------------------------------------------------------------------- diff --git a/core/functional/src/main/java/org/apache/zest/functional/ForEach.java b/core/functional/src/main/java/org/apache/zest/functional/ForEach.java deleted file mode 100644 index 23d1619..0000000 --- a/core/functional/src/main/java/org/apache/zest/functional/ForEach.java +++ /dev/null @@ -1,97 +0,0 @@ -/* - * Copyright (c) 2010, Rickard Ãberg. All Rights Reserved. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - */ -package org.apache.zest.functional; - -import java.util.Iterator; -import java.util.function.Function; -import java.util.function.Predicate; - -/** - * When using Iterables with map() and filter() the code often reads "in reverse", with the first item last in the code. - * Example: Iterables.map(function,Iterables.filter(specification, iterable)) - * <p> - * This ForEach class reverses that order and makes the code more readable, and allows easy application of visitors on iterables. - * </p> - * <p> - * Example: forEach(iterable).filter(specification).map(function).visit(visitor) - * </p> - */ -public final class ForEach<T> - implements Iterable<T> -{ - public static <T> ForEach<T> forEach( Iterable<T> iterable ) - { - return new ForEach<>( iterable ); - } - - private final Iterable<T> iterable; - - public ForEach( Iterable<T> iterable ) - { - this.iterable = iterable; - } - - @Override - public Iterator<T> iterator() - { - return iterable.iterator(); - } - - public ForEach<T> filter( Predicate<? super T> specification ) - { - return new ForEach<>( Iterables.filter( specification, iterable ) ); - } - - public <TO> ForEach<TO> map( Function<? /* super T */, TO> function ) - { - return new ForEach<>( Iterables.map( function, iterable ) ); - } - - public <TO> ForEach<TO> flatten() - { - Iterable<Iterable<TO>> original = iterable(); - Iterable<TO> iterable1 = Iterables.flattenIterables( original ); - return new ForEach<>( iterable1 ); - } - - @SuppressWarnings( "unchecked" ) - private <TO> Iterable<Iterable<TO>> iterable() - { - return (Iterable<Iterable<TO>>) iterable; - } - - public T last() - { - T lastItem = null; - for( T item : iterable ) - { - lastItem = item; - } - return lastItem; - } - - public <ThrowableType extends Throwable> boolean visit( final Visitor<T, ThrowableType> visitor ) - throws ThrowableType - { - for( T item : iterable ) - { - if( !visitor.visit( item ) ) - { - return false; - } - } - - return true; - } -} http://git-wip-us.apache.org/repos/asf/zest-java/blob/bd6fbad9/core/functional/src/main/java/org/apache/zest/functional/Functions.java ---------------------------------------------------------------------- diff --git a/core/functional/src/main/java/org/apache/zest/functional/Functions.java b/core/functional/src/main/java/org/apache/zest/functional/Functions.java deleted file mode 100644 index f5688db..0000000 --- a/core/functional/src/main/java/org/apache/zest/functional/Functions.java +++ /dev/null @@ -1,237 +0,0 @@ -/* - * Copyright (c) 2010, Rickard Ãberg. All Rights Reserved. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - */ -package org.apache.zest.functional; - -import java.util.Comparator; -import java.util.HashMap; -import java.util.Map; -import java.util.function.BiFunction; -import java.util.function.Function; -import java.util.function.Predicate; - -/** - * Utility functions. Combine these with methods in Iterables, for example. See FunctionsTest for usages. - */ -public final class Functions -{ - public static <A, B, C> BiFunction<Function<? super B, C>, Function<A, B>, Function<A, C>> compose() - { - return Functions::compose; - } - - /** - * compose(F1(M,T),F2(F,M)) = F1(F2(F)) -> T - * - * @param outer The outer/encapsulating function - * @param inner The inner/encapsulated function - * - * @return A function that is a composition of an outer and inner function. - */ - public static <FROM, MIDDLE, TO> Function<FROM, TO> compose( final Function<? super MIDDLE, TO> outer, - final Function<FROM, MIDDLE> inner - ) - { - return from -> outer.apply( inner.apply( from ) ); - } - - public static <TO, FROM extends TO> Function<FROM, TO> identity() - { - return from -> from; - } - - public static <FROM, TO> Function<FROM, TO> fromMap( final Map<FROM, TO> map ) - { - return map::get; - } - - public static <T> Function<T, T> withDefault( final T defaultValue ) - { - return from -> from == null ? defaultValue : from; - } - - public static Function<Number, Long> longSum() - { - return new Function<Number, Long>() - { - private long sum = 0; - - @Override - public Long apply( Number number ) - { - sum += number.longValue(); - return sum; - } - }; - } - - public static Function<Number, Integer> intSum() - { - return new Function<Number, Integer>() - { - private int sum = 0; - - @Override - public Integer apply( Number number ) - { - sum += number.intValue(); - return sum; - } - }; - } - - /** - * Count the number of items in an iterable that matches a given specification. - * - * Sample usage: last( apply( count( in( "X" ) ), iterable( "X","Y","X","X","Y" ) ) ) - * Returns: 3 - * - * @param specification The items that adhere to the Specification is counted. - * @param <T> The type of the items. - * - * @return A Function that can count items adhering to a Specification. - */ - public static <T> Function<T, Integer> count( final Predicate<T> specification ) - { - return new Function<T, Integer>() - { - int count; - - @Override - public Integer apply( T item ) - { - if( specification.test( item ) ) - { - count++; - } - - return count; - } - }; - } - - /** - * Find out the index of an item matching a given specification in an iterable. - * Returns -1 if it is not found. - * - * @param specification The Specification that specifies what to look for. - * @param <T> The type of the items. - * - * @return A Function that will provide the 'index' where the Specifcation is fulfilled. The Function will - * return -1 if the current item doesn't fulfill the Specification. - */ - public static <T> Function<T, Integer> indexOf( final Predicate<T> specification ) - { - return new Function<T, Integer>() - { - int index = -1; - int current = 0; - - @Override - public Integer apply( T item ) - { - if( index == -1 && specification.test( item ) ) - { - index = current; - } - - current++; - - return index; - } - }; - } - - /** - * Find out the index of an item in an iterable. - * - * @param item The item to look for. - * @param iterable The Iterable to search. - * @param <T> The type of the items. - * - * @return The index in the Iterable where the item is located. - */ - @SuppressWarnings( "unchecked" ) - public static <T> int indexOf( T item, Iterable<T> iterable ) - { - return Iterables.first( Iterables.filter( Specifications.not( Specifications.in( -1 ) ), - Iterables.map( indexOf( Specifications.in( item ) ), iterable ) ) ); - } - - /** - * Only apply given function on objects that satisfies the given specification. - * - * @param specification A Specification that specifies what should be included in the filtered result. - * @param function The function to be applied to items that fulfills the Specification - * @param <T> The type of the items. - * - * @return A Function that performs the filter operation when applied to Iterables. - */ - public static <T> Function<T, T> filteredMap( final Predicate<T> specification, final Function<T, T> function ) - { - return new Function<T, T>() - { - @Override - public T apply( T from ) - { - return specification.test( from ) ? function.apply( from ) : from; - } - }; - } - - /** - * Creates a comparator that takes a function as input. The returned comparator will use the - * function once for each item in the list to be sorted by Collections.sort. - * - * This should be used if the function to generate the sort key from an object is expensive, so - * that it is not done many times for each item in a list. - * - * @param comparableFunction The Function that the Comparator will delegate to. - * @param <T> The generic type to be used. - * - * @return A comparator that uses a Function for the compare operation. - */ - @SuppressWarnings( "raw" ) - public static <T> Comparator<T> comparator( final Function<T, Comparable> comparableFunction ) - { - return new Comparator<T>() - { - Map<T, Comparable> compareKeys = new HashMap<>(); - - @Override - @SuppressWarnings( "unchecked" ) - public int compare( T o1, T o2 ) - { - Comparable key1 = compareKeys.get( o1 ); - if( key1 == null ) - { - key1 = comparableFunction.apply( o1 ); - compareKeys.put( o1, key1 ); - } - - Comparable key2 = compareKeys.get( o2 ); - if( key2 == null ) - { - key2 = comparableFunction.apply( o2 ); - compareKeys.put( o2, key2 ); - } - - return key1.compareTo( key2 ); - } - }; - } - - private Functions() - { - } -} http://git-wip-us.apache.org/repos/asf/zest-java/blob/bd6fbad9/core/functional/src/main/java/org/apache/zest/functional/Iterables.java ---------------------------------------------------------------------- diff --git a/core/functional/src/main/java/org/apache/zest/functional/Iterables.java b/core/functional/src/main/java/org/apache/zest/functional/Iterables.java index bc6f076..6fec219 100644 --- a/core/functional/src/main/java/org/apache/zest/functional/Iterables.java +++ b/core/functional/src/main/java/org/apache/zest/functional/Iterables.java @@ -70,36 +70,6 @@ public final class Iterables return EMPTY; } - public static <T> Iterable<T> constant( final T item ) - { - return new Iterable<T>() - { - @Override - public Iterator<T> iterator() - { - return new Iterator<T>() - { - @Override - public boolean hasNext() - { - return true; - } - - @Override - public T next() - { - return item; - } - - @Override - public void remove() - { - } - }; - } - }; - } - public static <T> Iterable<T> limit( final int limitItems, final Iterable<T> iterable ) { return new Iterable<T>() @@ -453,59 +423,52 @@ public final class Iterables public static <T> Iterable<T> prepend( final T item, final Iterable<T> iterable ) { - return new Iterable<T>() + return () -> new Iterator<T>() { + private T first = item; + private Iterator<T> iterator; + @Override - public Iterator<T> iterator() + public boolean hasNext() { - return new Iterator<T>() + if( first != null ) { - T first = item; - Iterator<T> iterator; - - @Override - public boolean hasNext() + return true; + } + else + { + if( iterator == null ) { - if( first != null ) - { - return true; - } - else - { - if( iterator == null ) - { - iterator = iterable.iterator(); - } - } - - return iterator.hasNext(); + iterator = iterable.iterator(); } + } - @Override - public T next() + return iterator.hasNext(); + } + + @Override + public T next() + { + if( first != null ) + { + try { - if( first != null ) - { - try - { - return first; - } - finally - { - first = null; - } - } - else - { - return iterator.next(); - } + return first; } - - @Override - public void remove() + finally { + first = null; } - }; + } + else + { + return iterator.next(); + } + } + + @Override + public void remove() + { } }; } http://git-wip-us.apache.org/repos/asf/zest-java/blob/bd6fbad9/core/functional/src/main/java/org/apache/zest/functional/Specifications.java ---------------------------------------------------------------------- diff --git a/core/functional/src/main/java/org/apache/zest/functional/Specifications.java b/core/functional/src/main/java/org/apache/zest/functional/Specifications.java deleted file mode 100644 index 467f468..0000000 --- a/core/functional/src/main/java/org/apache/zest/functional/Specifications.java +++ /dev/null @@ -1,208 +0,0 @@ -/* - * Copyright (c) 2010, Rickard Ãberg. All Rights Reserved. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - */ -package org.apache.zest.functional; - -import java.util.function.Function; -import java.util.function.Predicate; - -/** - * Common generic specification expressions - */ -public class Specifications -{ - public static <T> Predicate<T> TRUE() - { - return new Predicate<T>() - { - @Override - public boolean test( T instance ) - { - return true; - } - }; - } - - public static <T> Predicate<T> not( final Predicate<T> specification ) - { - return new Predicate<T>() - { - @Override - public boolean test( T instance ) - { - return !specification.test( instance ); - } - }; - } - - @SafeVarargs - public static <T> AndSpecification<T> and( final Predicate<T>... specifications ) - { - return and( Iterables.iterable( specifications ) ); - } - - public static <T> AndSpecification<T> and( final Iterable<Predicate<T>> specifications ) - { - return new AndSpecification<>( specifications ); - } - - @SafeVarargs - public static <T> OrSpecification<T> or( final Predicate<T>... specifications ) - { - return or( Iterables.iterable( specifications ) ); - } - - public static <T> OrSpecification<T> or( final Iterable<Predicate<T>> specifications ) - { - return new OrSpecification<>( specifications ); - } - - @SafeVarargs - public static <T> Predicate<T> in( final T... allowed ) - { - return in( Iterables.iterable( allowed ) ); - } - - public static <T> Predicate<T> in( final Iterable<T> allowed ) - { - return new Predicate<T>() - { - @Override - public boolean test( T item ) - { - for( T allow : allowed ) - { - if( allow.equals( item ) ) - { - return true; - } - } - return false; - } - }; - } - - public static <T> Predicate<T> notNull() - { - return new Predicate<T>() - { - @Override - public boolean test( T item ) - { - return item != null; - } - }; - } - - public static <FROM, TO> Predicate<FROM> translate( final Function<FROM, TO> function, - final Predicate<? super TO> specification - ) - { - return new Predicate<FROM>() - { - @Override - public boolean test( FROM item ) - { - return specification.test( function.apply( item ) ); - } - }; - } - - /** - * AND Specification. - */ - public static class AndSpecification<T> - implements Predicate<T> - { - private final Iterable<Predicate<T>> specifications; - - private AndSpecification( Iterable<Predicate<T>> specifications ) - { - this.specifications = specifications; - } - - @Override - public boolean test( T instance ) - { - for( Predicate<T> specification : specifications ) - { - if( !specification.test( instance ) ) - { - return false; - } - } - - return true; - } - - @SafeVarargs - public final AndSpecification<T> and( Predicate<T>... specifications ) - { - Iterable<Predicate<T>> iterable = Iterables.iterable( specifications ); - Iterable<Predicate<T>> flatten = Iterables.flatten( this.specifications, iterable ); - return Specifications.and( flatten ); - } - - @SafeVarargs - public final OrSpecification<T> or( Predicate<T>... specifications ) - { - return Specifications.or( Iterables.prepend( this, Iterables.iterable( specifications ) ) ); - } - } - - /** - * OR Specification. - */ - public static class OrSpecification<T> - implements Predicate<T> - { - private final Iterable<Predicate<T>> specifications; - - private OrSpecification( Iterable<Predicate<T>> specifications ) - { - this.specifications = specifications; - } - - @Override - public boolean test( T instance ) - { - for( Predicate<T> specification : specifications ) - { - if( specification.test( instance ) ) - { - return true; - } - } - - return false; - } - - @SafeVarargs - public final AndSpecification<T> and( Predicate<T>... specifications ) - { - return Specifications.and( Iterables.prepend( this, Iterables.iterable( specifications ) ) ); - } - - @SafeVarargs - public final OrSpecification<T> or( Predicate<T>... specifications ) - { - Iterable<Predicate<T>> iterable = Iterables.iterable( specifications ); - Iterable<Predicate<T>> flatten = Iterables.flatten( this.specifications, iterable ); - return Specifications.or( flatten ); - } - } - - private Specifications() - { - } -} http://git-wip-us.apache.org/repos/asf/zest-java/blob/bd6fbad9/core/functional/src/test/java/org/apache/zest/functional/FunctionsTest.java ---------------------------------------------------------------------- diff --git a/core/functional/src/test/java/org/apache/zest/functional/FunctionsTest.java b/core/functional/src/test/java/org/apache/zest/functional/FunctionsTest.java deleted file mode 100644 index bb2428c..0000000 --- a/core/functional/src/test/java/org/apache/zest/functional/FunctionsTest.java +++ /dev/null @@ -1,144 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ -package org.apache.zest.functional; - -import java.util.Collections; -import java.util.Comparator; -import java.util.HashMap; -import java.util.List; -import java.util.Map; -import java.util.function.Function; -import org.junit.Test; - -import static org.hamcrest.CoreMatchers.equalTo; -import static org.junit.Assert.assertThat; -import static org.apache.zest.functional.ForEach.forEach; -import static org.apache.zest.functional.Functions.compose; -import static org.apache.zest.functional.Functions.count; -import static org.apache.zest.functional.Functions.indexOf; -import static org.apache.zest.functional.Functions.intSum; -import static org.apache.zest.functional.Functions.longSum; -import static org.apache.zest.functional.Iterables.iterable; -import static org.apache.zest.functional.Iterables.last; -import static org.apache.zest.functional.Iterables.map; -import static org.apache.zest.functional.Specifications.in; - -/** - * Test of utility functions - */ -public class FunctionsTest -{ - Function<Object, String> stringifier = new Function<Object, String>() - { - @Override - public String apply( Object s ) - { - return s.toString(); - } - }; - - Function<String, Integer> length = new Function<String, Integer>() - { - @Override - public Integer apply( String s ) - { - return s.length(); - } - }; - - @Test - public void testCompose() - { - assertThat( Functions.<Object, String, Integer>compose() - .apply( length, stringifier ) - .apply( 12345L ), equalTo( 5 ) ); - assertThat( compose( length, stringifier ).apply( 12345L ), equalTo( 5 ) ); - } - - @Test - public void testFromMap() - { - Map<String, String> map = new HashMap<String, String>(); - map.put( "A", "1" ); - map.put( "B", "2" ); - map.put( "C", "3" ); - assertThat( Iterables.toList( Iterables.filter( Specifications.notNull(), Iterables.map( Functions.fromMap( map ), Iterables - .iterable( "A", "B", "D" ) ) ) ).toString(), equalTo( "[1, 2]" ) ); - } - - @Test - public void testWithDefault() - { - assertThat( Iterables.toList( Iterables.map( Functions.withDefault( "DEFAULT" ), Iterables.iterable( "123", null, "456" ) ) ) - .toString(), equalTo( "[123, DEFAULT, 456]" ) ); - } - - @Test - public void testLongSum() - { - assertThat( last( map( longSum(), iterable( 1, 2L, 3F, 4D ) ) ), equalTo( 10L ) ); - } - - @Test - public void testLongSum2() - { - assertThat( forEach( iterable( 1, 2, 3, 4 ) ).map( longSum() ).last(), equalTo( 10L ) ); - } - - @Test - public void testIntSum() - { - assertThat( last( map( intSum(), iterable( 1, 2L, 3F, 4D ) ) ), equalTo( 10 ) ); - } - - @Test - public void testCount() - { - assertThat( last( map( count( in( "X" ) ), iterable( "X", "Y", "X", "X", "Y" ) ) ), equalTo( 3 ) ); - } - - @Test - public void testIndexOf() - { - assertThat( last( map( indexOf( in( "D" ) ), iterable( "A", "B", "C", "D", "D" ) ) ), equalTo( 3 ) ); - } - - @Test - public void testIndexOf2() - { - assertThat( indexOf( "D", iterable( "A", "B", "C", "D", "D" ) ), equalTo( 3 ) ); - } - - @Test - public void testComparator() - { - Comparator<Integer> comparator = Functions.comparator( new Function<Integer, Comparable>() - { - @Override - public Comparable apply( Integer integer ) - { - return integer.toString(); - } - } ); - Iterable<Integer> iterable = Iterables.iterable( 1, 5, 3, 6, 8 ); - List<Integer> integers = Iterables.toList( iterable ); - Collections.sort( integers, comparator ); - assertThat( integers.toString(), equalTo( "[1, 3, 5, 6, 8]" ) ); - } -} http://git-wip-us.apache.org/repos/asf/zest-java/blob/bd6fbad9/core/functional/src/test/java/org/apache/zest/functional/IterablesTest.java ---------------------------------------------------------------------- diff --git a/core/functional/src/test/java/org/apache/zest/functional/IterablesTest.java b/core/functional/src/test/java/org/apache/zest/functional/IterablesTest.java index 7ff5bab..f8512e9 100644 --- a/core/functional/src/test/java/org/apache/zest/functional/IterablesTest.java +++ b/core/functional/src/test/java/org/apache/zest/functional/IterablesTest.java @@ -19,7 +19,6 @@ import java.util.Comparator; import java.util.Enumeration; import java.util.List; import java.util.function.Function; -import java.util.function.Predicate; import org.hamcrest.CoreMatchers; import org.junit.Test; @@ -38,19 +37,6 @@ public class IterablesTest private Iterable<Integer> numberIntegers = Arrays.asList( 1, 2, 3 ); @Test - public void testConstant() - { - String str = ""; - - for( String string : Iterables.limit( 3, Iterables.constant( "123" ) ) ) - { - str += string; - } - - assertThat( str, CoreMatchers.equalTo( "123123123" ) ); - } - - @Test public void testUnique() { String str = ""; @@ -77,12 +63,6 @@ public class IterablesTest } @Test - public void testFilter() - { - assertThat( Iterables.first( Iterables.filter( Specifications.in( "2" ), numbers ) ), equalTo( "2" ) ); - } - - @Test public void testFirst() { assertThat( Iterables.first( numbers ), equalTo( "1" ) ); @@ -128,20 +108,6 @@ public class IterablesTest } @Test - public void testMatchesAny() - { - assertThat( Iterables.matchesAny( Specifications.in( "2" ), numbers ), equalTo( true ) ); - assertThat( Iterables.matchesAny( Specifications.in( "4" ), numbers ), equalTo( false ) ); - } - - @Test - public void testMatchesAll() - { - assertThat( Iterables.matchesAll( Specifications.in( "1", "2", "3" ), numbers ), equalTo( true ) ); - assertThat( Iterables.matchesAll( Specifications.in( "2", "3", "4" ), numbers ), equalTo( false ) ); - } - - @Test public void testFlatten() { assertThat( Iterables.toList( Iterables.flatten( numbers, numbers ) ).toString(), @@ -218,62 +184,6 @@ public class IterablesTest } @Test - public void testDebug() - { - assertThat( Iterables.first( Iterables.debug( "Filtered number:{0}", - Iterables.filter( Specifications.in( "2" ), - Iterables.debug( "Number:{0}", numbers ) ) ) ), - equalTo( "2" ) ); - } - - @Test - public void testDebugWithFunctions() - { - Function<String, String> fun = new Function<String, String>() - { - - @Override - public String apply( String s ) - { - return s + ":" + s.length(); - } - - }; - assertThat( Iterables.first( Iterables.debug( "Filtered number:{0}", - Iterables.filter( Specifications.in( "2" ), - Iterables.debug( "Number:{0}", numbers, fun ) ) ) ), - equalTo( "2" ) ); - } - - @Test - public void testCache() - { - final int[] count = new int[ 1 ]; - - Iterable<String> b = Iterables.cache( Iterables.filter( Specifications.and( new Predicate<String>() - { - - @Override - public boolean test( String item ) - { - count[ 0] = count[ 0] + 1; - return true; - } - - }, Specifications.in( "B" ) ), Iterables.iterable( "A", "B", "C" ) ) ); - - assertThat( count[ 0], equalTo( 0 ) ); - - Iterables.toList( b ); - - assertThat( count[ 0], equalTo( 3 ) ); - - Iterables.toList( b ); - - assertThat( count[ 0], equalTo( 3 ) ); - } - - @Test public void testSort() { assertThat( Iterables.sort( Iterables.reverse( numberLongs ) ).toString(), equalTo( "[1, 2, 3]" ) ); http://git-wip-us.apache.org/repos/asf/zest-java/blob/bd6fbad9/core/functional/src/test/java/org/apache/zest/functional/SpecificationsTest.java ---------------------------------------------------------------------- diff --git a/core/functional/src/test/java/org/apache/zest/functional/SpecificationsTest.java b/core/functional/src/test/java/org/apache/zest/functional/SpecificationsTest.java deleted file mode 100644 index f5ce2c0..0000000 --- a/core/functional/src/test/java/org/apache/zest/functional/SpecificationsTest.java +++ /dev/null @@ -1,86 +0,0 @@ -/* - * Copyright (c) 2010, Rickard Ãberg. All Rights Reserved. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - */ - -package org.apache.zest.functional; - -import java.util.function.Function; -import java.util.function.Predicate; -import org.junit.Assert; -import org.junit.Test; - -import static org.hamcrest.CoreMatchers.equalTo; - -/** - * JAVADOC - */ -public class SpecificationsTest -{ - @Test - public void testTRUE() - { - Assert.assertThat( Specifications.<Object>TRUE().test( new Object() ), equalTo( true ) ); - } - - @Test - public void testNot() - { - Assert.assertThat( Specifications.not( Specifications.<Object>TRUE() ).test( new Object() ), equalTo( false ) ); - } - - @Test - public void testAnd() - { - Predicate<Object> trueSpec = Specifications.<Object>TRUE(); - Predicate<Object> falseSpec = Specifications.not( Specifications.<Object>TRUE() ); - - Assert.assertThat( Specifications.and( falseSpec, falseSpec ).test( new Object() ), equalTo( false ) ); - Assert.assertThat( Specifications.and( trueSpec, falseSpec ).test( new Object() ), equalTo( false ) ); - Assert.assertThat( Specifications.and( falseSpec, trueSpec ).test( new Object() ), equalTo( false ) ); - Assert.assertThat( Specifications.and( trueSpec, trueSpec ).test( new Object() ), equalTo( true ) ); - } - - @Test - public void testOr() - { - Predicate<Object> trueSpec = Specifications.<Object>TRUE(); - Predicate<Object> falseSpec = Specifications.not( Specifications.<Object>TRUE() ); - - Assert.assertThat( Specifications.or( falseSpec, falseSpec ).test( new Object() ), equalTo( false ) ); - Assert.assertThat( Specifications.or( trueSpec, falseSpec ).test( new Object() ), equalTo( true ) ); - Assert.assertThat( Specifications.or( falseSpec, trueSpec ).test( new Object() ), equalTo( true ) ); - Assert.assertThat( Specifications.or( trueSpec, trueSpec ).test( new Object() ), equalTo( true ) ); - } - - @Test - public void testIn() - { - Assert.assertThat( Specifications.in( "1", "2", "3" ).test( "2" ), equalTo( true ) ); - Assert.assertThat( Specifications.in( "1", "2", "3" ).test( "4" ), equalTo( false ) ); - } - - @Test - public void testTranslate() - { - Function<Object, String> stringifier = new Function<Object, String>() - { - @Override - public String apply( Object s ) - { - return s.toString(); - } - }; - - Assert.assertTrue( Specifications.translate( stringifier, Specifications.in( "3" ) ).test( 3L ) ); - } -} http://git-wip-us.apache.org/repos/asf/zest-java/blob/bd6fbad9/core/functional/src/test/java/org/apache/zest/functional/docsupport/FunctionalDocs.java ---------------------------------------------------------------------- diff --git a/core/functional/src/test/java/org/apache/zest/functional/docsupport/FunctionalDocs.java b/core/functional/src/test/java/org/apache/zest/functional/docsupport/FunctionalDocs.java index 12a218d..15e9a60 100644 --- a/core/functional/src/test/java/org/apache/zest/functional/docsupport/FunctionalDocs.java +++ b/core/functional/src/test/java/org/apache/zest/functional/docsupport/FunctionalDocs.java @@ -19,11 +19,7 @@ package org.apache.zest.functional.docsupport; import java.util.ArrayList; - -// START SNIPPET: func2 -import static org.apache.zest.functional.ForEach.forEach; -import static org.apache.zest.functional.Functions.longSum; -// END SNIPPET: func2 +import java.util.stream.StreamSupport; public class FunctionalDocs { @@ -45,9 +41,9 @@ public class FunctionalDocs } { // START SNIPPET: func2 - Iterable<Number> data = new ArrayList<Number>(); - Long sum = forEach( data ).map( longSum() ).last(); - System.out.println( "The sum is " + sum ); + Iterable<Long> data = new ArrayList<>(); + Long total = StreamSupport.stream( data.spliterator(), true ).reduce( 0L, ( sum, n ) -> sum + n ); + System.out.println( "The sum is " + total ); // END SNIPPET: func2 } http://git-wip-us.apache.org/repos/asf/zest-java/blob/bd6fbad9/core/runtime/src/main/java/org/apache/zest/runtime/activation/ActivationDelegate.java ---------------------------------------------------------------------- diff --git a/core/runtime/src/main/java/org/apache/zest/runtime/activation/ActivationDelegate.java b/core/runtime/src/main/java/org/apache/zest/runtime/activation/ActivationDelegate.java index e7406b2..b69d9a7 100644 --- a/core/runtime/src/main/java/org/apache/zest/runtime/activation/ActivationDelegate.java +++ b/core/runtime/src/main/java/org/apache/zest/runtime/activation/ActivationDelegate.java @@ -18,6 +18,7 @@ import java.util.Collections; import java.util.LinkedHashSet; import java.util.LinkedList; import java.util.Set; +import java.util.stream.Stream; import org.apache.zest.api.activation.Activation; import org.apache.zest.api.activation.ActivationEvent; import org.apache.zest.api.activation.ActivationEventListener; @@ -156,7 +157,7 @@ public final class ActivationDelegate public void passivate() throws PassivationException { - passivate( (Runnable) null ); + passivate( null ); } @SuppressWarnings( "unchecked" ) @@ -221,14 +222,7 @@ public final class ActivationDelegate } catch( Exception ex ) { - if( ex instanceof PassivationException ) - { - exceptions.addAll( ( (PassivationException) ex ).causes() ); - } - else - { - exceptions.add( ex ); - } + exceptions.add( ex ); } } @@ -340,7 +334,7 @@ public final class ActivationDelegate } @Override - public Iterable<Class<?>> types() + public Stream<Class<?>> types() { return reference.types(); } http://git-wip-us.apache.org/repos/asf/zest-java/blob/bd6fbad9/core/runtime/src/main/java/org/apache/zest/runtime/association/AssociationModel.java ---------------------------------------------------------------------- diff --git a/core/runtime/src/main/java/org/apache/zest/runtime/association/AssociationModel.java b/core/runtime/src/main/java/org/apache/zest/runtime/association/AssociationModel.java index 323b5aa..3558d6e 100644 --- a/core/runtime/src/main/java/org/apache/zest/runtime/association/AssociationModel.java +++ b/core/runtime/src/main/java/org/apache/zest/runtime/association/AssociationModel.java @@ -21,12 +21,12 @@ import java.lang.reflect.Method; import java.lang.reflect.Type; import java.lang.reflect.TypeVariable; import java.util.List; +import java.util.stream.Stream; import org.apache.zest.api.association.Association; import org.apache.zest.api.association.AssociationDescriptor; import org.apache.zest.api.association.GenericAssociationInfo; import org.apache.zest.api.common.MetaInfo; import org.apache.zest.api.common.QualifiedName; -import org.apache.zest.api.composite.Composite; import org.apache.zest.api.constraint.ConstraintViolation; import org.apache.zest.api.constraint.ConstraintViolationException; import org.apache.zest.api.entity.Aggregated; @@ -40,9 +40,6 @@ import org.apache.zest.runtime.composite.ValueConstraintsInstance; import org.apache.zest.runtime.model.Binder; import org.apache.zest.runtime.model.Resolution; -import static org.apache.zest.functional.Iterables.empty; -import static org.apache.zest.functional.Iterables.first; - /** * Model for an Association. * @@ -149,7 +146,7 @@ public final class AssociationModel List<ConstraintViolation> violations = constraints.checkConstraints( value ); if( !violations.isEmpty() ) { - Iterable<Class<?>> empty = empty(); + Stream<Class<?>> empty = Stream.empty(); throw new ConstraintViolationException( "", empty, (Member) accessor, violations ); } } @@ -163,7 +160,8 @@ public final class AssociationModel List<ConstraintViolation> violations = associationConstraints.checkConstraints( association ); if( !violations.isEmpty() ) { - throw new ConstraintViolationException( (Composite) association.get(), (Member) accessor, violations ); + Stream<Class<?>> empty = Stream.empty(); + throw new ConstraintViolationException( "", empty, (Member) accessor, violations ); } } } @@ -203,7 +201,7 @@ public final class AssociationModel if( type instanceof TypeVariable ) { - Class mainType = first( resolution.model().types() ); + Class mainType = resolution.model().types().findFirst().orElse( null ); type = Classes.resolveTypeVariable( (TypeVariable) type, ( (Member) accessor ).getDeclaringClass(), mainType ); } } http://git-wip-us.apache.org/repos/asf/zest-java/blob/bd6fbad9/core/runtime/src/main/java/org/apache/zest/runtime/association/AssociationsModel.java ---------------------------------------------------------------------- diff --git a/core/runtime/src/main/java/org/apache/zest/runtime/association/AssociationsModel.java b/core/runtime/src/main/java/org/apache/zest/runtime/association/AssociationsModel.java index a0f3d67..2932d2f 100644 --- a/core/runtime/src/main/java/org/apache/zest/runtime/association/AssociationsModel.java +++ b/core/runtime/src/main/java/org/apache/zest/runtime/association/AssociationsModel.java @@ -21,6 +21,7 @@ import java.lang.reflect.AccessibleObject; import java.lang.reflect.Member; import java.util.LinkedHashMap; import java.util.Map; +import java.util.stream.Stream; import org.apache.zest.api.association.Association; import org.apache.zest.api.association.AssociationDescriptor; import org.apache.zest.api.association.AssociationStateHolder; @@ -40,9 +41,9 @@ public final class AssociationsModel { } - public Iterable<AssociationModel> associations() + public Stream<AssociationModel> associations() { - return mapAccessorAssociationModel.values(); + return mapAccessorAssociationModel.values().stream(); } public void addAssociation( AssociationModel associationModel ) http://git-wip-us.apache.org/repos/asf/zest-java/blob/bd6fbad9/core/runtime/src/main/java/org/apache/zest/runtime/association/ManyAssociationModel.java ---------------------------------------------------------------------- diff --git a/core/runtime/src/main/java/org/apache/zest/runtime/association/ManyAssociationModel.java b/core/runtime/src/main/java/org/apache/zest/runtime/association/ManyAssociationModel.java index 73fa70c..57c6790 100644 --- a/core/runtime/src/main/java/org/apache/zest/runtime/association/ManyAssociationModel.java +++ b/core/runtime/src/main/java/org/apache/zest/runtime/association/ManyAssociationModel.java @@ -25,6 +25,7 @@ import java.lang.reflect.Type; import java.lang.reflect.TypeVariable; import java.util.List; import java.util.function.BiFunction; +import java.util.stream.Stream; import org.apache.zest.api.association.AssociationDescriptor; import org.apache.zest.api.association.GenericAssociationInfo; import org.apache.zest.api.association.ManyAssociation; @@ -161,7 +162,7 @@ public final class ManyAssociationModel List<ConstraintViolation> violations = constraints.checkConstraints( composite ); if( !violations.isEmpty() ) { - Iterable<Class<?>> empty = empty(); + Stream<Class<?>> empty = Stream.empty(); throw new ConstraintViolationException( "", empty, (Member) accessor, violations ); } } @@ -175,7 +176,7 @@ public final class ManyAssociationModel List<ConstraintViolation> violations = associationConstraints.checkConstraints( manyAssociation ); if( !violations.isEmpty() ) { - Iterable<Class<?>> empty = empty(); + Stream<Class<?>> empty = Stream.empty(); throw new ConstraintViolationException( "", empty, (Member) accessor, violations ); } } @@ -222,7 +223,7 @@ public final class ManyAssociationModel if( type instanceof TypeVariable ) { - Class mainType = first( resolution.model().types() ); + Class mainType = resolution.model().types().findFirst().orElse( null ); type = Classes.resolveTypeVariable( (TypeVariable) type, ( (Member) accessor ).getDeclaringClass(), mainType ); } } http://git-wip-us.apache.org/repos/asf/zest-java/blob/bd6fbad9/core/runtime/src/main/java/org/apache/zest/runtime/association/ManyAssociationsModel.java ---------------------------------------------------------------------- diff --git a/core/runtime/src/main/java/org/apache/zest/runtime/association/ManyAssociationsModel.java b/core/runtime/src/main/java/org/apache/zest/runtime/association/ManyAssociationsModel.java index 220d3ec..11d188d 100644 --- a/core/runtime/src/main/java/org/apache/zest/runtime/association/ManyAssociationsModel.java +++ b/core/runtime/src/main/java/org/apache/zest/runtime/association/ManyAssociationsModel.java @@ -21,6 +21,7 @@ import java.lang.reflect.AccessibleObject; import java.lang.reflect.Member; import java.util.LinkedHashMap; import java.util.Map; +import java.util.stream.Stream; import org.apache.zest.api.association.AssociationDescriptor; import org.apache.zest.api.association.ManyAssociation; import org.apache.zest.api.common.QualifiedName; @@ -42,9 +43,9 @@ public final class ManyAssociationsModel { } - public Iterable<ManyAssociationModel> manyAssociations() + public Stream<ManyAssociationModel> manyAssociations() { - return mapAccessorAssociationModel.values(); + return mapAccessorAssociationModel.values().stream(); } public void addManyAssociation( ManyAssociationModel model ) http://git-wip-us.apache.org/repos/asf/zest-java/blob/bd6fbad9/core/runtime/src/main/java/org/apache/zest/runtime/association/NamedAssociationModel.java ---------------------------------------------------------------------- diff --git a/core/runtime/src/main/java/org/apache/zest/runtime/association/NamedAssociationModel.java b/core/runtime/src/main/java/org/apache/zest/runtime/association/NamedAssociationModel.java index 82a7af5..0b0f340 100644 --- a/core/runtime/src/main/java/org/apache/zest/runtime/association/NamedAssociationModel.java +++ b/core/runtime/src/main/java/org/apache/zest/runtime/association/NamedAssociationModel.java @@ -26,6 +26,7 @@ import java.lang.reflect.Type; import java.lang.reflect.TypeVariable; import java.util.List; import java.util.function.BiFunction; +import java.util.stream.Stream; import org.apache.zest.api.association.AssociationDescriptor; import org.apache.zest.api.association.GenericAssociationInfo; import org.apache.zest.api.association.NamedAssociation; @@ -162,7 +163,7 @@ public final class NamedAssociationModel List<ConstraintViolation> violations = constraints.checkConstraints( composite ); if( !violations.isEmpty() ) { - Iterable<Class<?>> empty = empty(); + Stream<Class<?>> empty = Stream.empty(); throw new ConstraintViolationException( "", empty, (Member) accessor, violations ); } } @@ -176,7 +177,7 @@ public final class NamedAssociationModel List<ConstraintViolation> violations = associationConstraints.checkConstraints( association ); if( !violations.isEmpty() ) { - Iterable<Class<?>> empty = empty(); + Stream<Class<?>> empty = Stream.empty(); throw new ConstraintViolationException( "", empty, (Member) accessor, violations ); } } @@ -223,7 +224,7 @@ public final class NamedAssociationModel if( type instanceof TypeVariable ) { - Class mainType = first( resolution.model().types() ); + Class mainType = resolution.model().types().findFirst().orElse( null ); type = Classes.resolveTypeVariable( (TypeVariable) type, ( (Member) accessor ).getDeclaringClass(), mainType ); } } http://git-wip-us.apache.org/repos/asf/zest-java/blob/bd6fbad9/core/runtime/src/main/java/org/apache/zest/runtime/association/NamedAssociationsModel.java ---------------------------------------------------------------------- diff --git a/core/runtime/src/main/java/org/apache/zest/runtime/association/NamedAssociationsModel.java b/core/runtime/src/main/java/org/apache/zest/runtime/association/NamedAssociationsModel.java index 87722b0..ad536d1 100644 --- a/core/runtime/src/main/java/org/apache/zest/runtime/association/NamedAssociationsModel.java +++ b/core/runtime/src/main/java/org/apache/zest/runtime/association/NamedAssociationsModel.java @@ -22,6 +22,7 @@ import java.lang.reflect.AccessibleObject; import java.lang.reflect.Member; import java.util.LinkedHashMap; import java.util.Map; +import java.util.stream.Stream; import org.apache.zest.api.association.AssociationDescriptor; import org.apache.zest.api.association.NamedAssociation; import org.apache.zest.api.common.QualifiedName; @@ -43,9 +44,9 @@ public final class NamedAssociationsModel { } - public Iterable<NamedAssociationModel> namedAssociations() + public Stream<NamedAssociationModel> namedAssociations() { - return mapAccessorAssociationModel.values(); + return mapAccessorAssociationModel.values().stream(); } public void addNamedAssociation( NamedAssociationModel model )
