http://git-wip-us.apache.org/repos/asf/polygene-java/blob/a789141d/core/bootstrap/src/test/java/org/qi4j/bootstrap/assembly/infrastructure/IndexingModule.java ---------------------------------------------------------------------- diff --git a/core/bootstrap/src/test/java/org/qi4j/bootstrap/assembly/infrastructure/IndexingModule.java b/core/bootstrap/src/test/java/org/qi4j/bootstrap/assembly/infrastructure/IndexingModule.java new file mode 100644 index 0000000..437f4f9 --- /dev/null +++ b/core/bootstrap/src/test/java/org/qi4j/bootstrap/assembly/infrastructure/IndexingModule.java @@ -0,0 +1,44 @@ +/* + * 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.qi4j.bootstrap.assembly.infrastructure; + +import org.qi4j.api.common.Visibility; +import org.qi4j.bootstrap.AssemblyException; +import org.qi4j.bootstrap.LayerAssembly; +import org.qi4j.bootstrap.ModuleAssembly; +import org.qi4j.bootstrap.layered.ModuleAssembler; + +public class IndexingModule + implements ModuleAssembler +{ + public static final String NAME = "Indexing Module"; + private final ModuleAssembly configModule; + + public IndexingModule( ModuleAssembly configModule ) + { + this.configModule = configModule; + } + + @Override + public ModuleAssembly assemble( LayerAssembly layer, ModuleAssembly module ) + throws AssemblyException + { + return module; + } +}
http://git-wip-us.apache.org/repos/asf/polygene-java/blob/a789141d/core/bootstrap/src/test/java/org/qi4j/bootstrap/assembly/infrastructure/InfrastructureLayer.java ---------------------------------------------------------------------- diff --git a/core/bootstrap/src/test/java/org/qi4j/bootstrap/assembly/infrastructure/InfrastructureLayer.java b/core/bootstrap/src/test/java/org/qi4j/bootstrap/assembly/infrastructure/InfrastructureLayer.java new file mode 100644 index 0000000..a75ef35 --- /dev/null +++ b/core/bootstrap/src/test/java/org/qi4j/bootstrap/assembly/infrastructure/InfrastructureLayer.java @@ -0,0 +1,47 @@ +/* + * 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.qi4j.bootstrap.assembly.infrastructure; + +import org.qi4j.bootstrap.AssemblyException; +import org.qi4j.bootstrap.LayerAssembly; +import org.qi4j.bootstrap.ModuleAssembly; +import org.qi4j.bootstrap.layered.LayerAssembler; +import org.qi4j.bootstrap.layered.LayeredLayerAssembler; + +public class InfrastructureLayer extends LayeredLayerAssembler + implements LayerAssembler +{ + public static final String NAME = "Infrastructure Layer"; + private final ModuleAssembly configModule; + + public InfrastructureLayer( ModuleAssembly configModule ) + { + this.configModule = configModule; + } + + @Override + public LayerAssembly assemble( LayerAssembly layer ) + throws AssemblyException + { + new StorageModule( configModule ).assemble( layer, layer.module( StorageModule.NAME ) ); + new IndexingModule( configModule ).assemble( layer, layer.module( IndexingModule.NAME ) ); + createModule( layer, SerializationModule.class ); + return layer; + } +} http://git-wip-us.apache.org/repos/asf/polygene-java/blob/a789141d/core/bootstrap/src/test/java/org/qi4j/bootstrap/assembly/infrastructure/SerializationModule.java ---------------------------------------------------------------------- diff --git a/core/bootstrap/src/test/java/org/qi4j/bootstrap/assembly/infrastructure/SerializationModule.java b/core/bootstrap/src/test/java/org/qi4j/bootstrap/assembly/infrastructure/SerializationModule.java new file mode 100644 index 0000000..ad33c16 --- /dev/null +++ b/core/bootstrap/src/test/java/org/qi4j/bootstrap/assembly/infrastructure/SerializationModule.java @@ -0,0 +1,36 @@ +/* + * 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.qi4j.bootstrap.assembly.infrastructure; + +import org.qi4j.bootstrap.AssemblyException; +import org.qi4j.bootstrap.LayerAssembly; +import org.qi4j.bootstrap.ModuleAssembly; +import org.qi4j.bootstrap.layered.ModuleAssembler; + +public class SerializationModule + implements ModuleAssembler +{ + @Override + public ModuleAssembly assemble( LayerAssembly layer, ModuleAssembly module + ) + throws AssemblyException + { + return null; + } +} http://git-wip-us.apache.org/repos/asf/polygene-java/blob/a789141d/core/bootstrap/src/test/java/org/qi4j/bootstrap/assembly/infrastructure/StorageModule.java ---------------------------------------------------------------------- diff --git a/core/bootstrap/src/test/java/org/qi4j/bootstrap/assembly/infrastructure/StorageModule.java b/core/bootstrap/src/test/java/org/qi4j/bootstrap/assembly/infrastructure/StorageModule.java new file mode 100644 index 0000000..0b51230 --- /dev/null +++ b/core/bootstrap/src/test/java/org/qi4j/bootstrap/assembly/infrastructure/StorageModule.java @@ -0,0 +1,44 @@ +/* + * 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.qi4j.bootstrap.assembly.infrastructure; + +import org.qi4j.api.common.Visibility; +import org.qi4j.bootstrap.AssemblyException; +import org.qi4j.bootstrap.LayerAssembly; +import org.qi4j.bootstrap.ModuleAssembly; +import org.qi4j.bootstrap.layered.ModuleAssembler; + +public class StorageModule + implements ModuleAssembler +{ + public static final String NAME = "Storage Module"; + private final ModuleAssembly configModule; + + public StorageModule( ModuleAssembly configModule ) + { + this.configModule = configModule; + } + + @Override + public ModuleAssembly assemble( LayerAssembly layer, ModuleAssembly module ) + throws AssemblyException + { + return module; + } +} http://git-wip-us.apache.org/repos/asf/polygene-java/blob/a789141d/core/bootstrap/src/test/java/org/qi4j/bootstrap/assembly/service/ServiceLayer.java ---------------------------------------------------------------------- diff --git a/core/bootstrap/src/test/java/org/qi4j/bootstrap/assembly/service/ServiceLayer.java b/core/bootstrap/src/test/java/org/qi4j/bootstrap/assembly/service/ServiceLayer.java new file mode 100644 index 0000000..85e29a1 --- /dev/null +++ b/core/bootstrap/src/test/java/org/qi4j/bootstrap/assembly/service/ServiceLayer.java @@ -0,0 +1,35 @@ +/* + * 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.qi4j.bootstrap.assembly.service; + +import org.qi4j.bootstrap.AssemblyException; +import org.qi4j.bootstrap.layered.LayerAssembler; +import org.qi4j.bootstrap.LayerAssembly; + +public class ServiceLayer implements LayerAssembler +{ + public static final String NAME = "Service"; + + @Override + public LayerAssembly assemble( LayerAssembly layer ) + throws AssemblyException + { + return null; + } +} http://git-wip-us.apache.org/repos/asf/polygene-java/blob/a789141d/core/bootstrap/src/test/java/org/qi4j/bootstrap/builder/ApplicationBuilderTest.java ---------------------------------------------------------------------- diff --git a/core/bootstrap/src/test/java/org/qi4j/bootstrap/builder/ApplicationBuilderTest.java b/core/bootstrap/src/test/java/org/qi4j/bootstrap/builder/ApplicationBuilderTest.java new file mode 100644 index 0000000..56f17f7 --- /dev/null +++ b/core/bootstrap/src/test/java/org/qi4j/bootstrap/builder/ApplicationBuilderTest.java @@ -0,0 +1,133 @@ +/* + * Copyright 2014 Niclas Hedhman. + * Copyright 2014 Paul Merlin. + * + * 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.qi4j.bootstrap.builder; + +import java.io.ByteArrayInputStream; +import java.io.IOException; +import java.io.InputStream; +import org.json.JSONException; +import org.junit.Test; +import org.qi4j.api.activation.ActivationException; +import org.qi4j.api.mixin.Mixins; +import org.qi4j.api.structure.Application; +import org.qi4j.api.structure.Module; +import org.qi4j.bootstrap.Assembler; +import org.qi4j.bootstrap.AssemblyException; +import org.qi4j.bootstrap.ModuleAssembly; + +import static org.hamcrest.core.IsEqual.equalTo; +import static org.junit.Assert.assertThat; +import static org.qi4j.bootstrap.ClassScanner.findClasses; +import static org.qi4j.bootstrap.ClassScanner.matches; +import static org.qi4j.functional.Iterables.filter; + +public class ApplicationBuilderTest +{ + @Test + public void givenBuilderUseWhenBuildingApplicationExpectSuccess() + throws AssemblyException, ActivationException + { + ApplicationBuilder builder = new ApplicationBuilder( "Build from API test." ); + builder.withLayer( "layer1" ).using( "layer2" ).using( "layer3" ); + builder.withLayer( "layer2" ); + builder.withLayer( "layer3" ).withModule( "test module" ). + withAssemblers( filter( matches( ".*ServiceAssembler" ), findClasses( getClass() ) ) ); + Application application = builder.newApplication(); + Module module = application.findModule( "layer3", "test module" ); + TestService service = module.findService( TestService.class ).get(); + assertThat( service.sayHello(), equalTo( "Hello Zest!" ) ); + } + + @Test + public void givenJsonWhenBuildingApplicationExpectSuccess() + throws JSONException, ActivationException, AssemblyException + { + ApplicationBuilder builder = ApplicationBuilder.fromJson( APPLICATION ); + Application application = builder.newApplication(); + Module module = application.findModule( "layer3", "test module" ); + TestService service = module.findService( TestService.class ).get(); + assertThat( service.sayHello(), equalTo( "Hello Zest!" ) ); + } + + @Test + public void givenJsonInputStreamWhenBuildingApplicationExpectSuccess() + throws IOException, JSONException, ActivationException, AssemblyException + { + InputStream input = new ByteArrayInputStream( APPLICATION.getBytes( "UTF-8" ) ); + ApplicationBuilder builder = ApplicationBuilder.fromJson( input ); + Application application = builder.newApplication(); + Module module = application.findModule( "layer3", "test module" ); + TestService service = module.findService( TestService.class ).get(); + assertThat( service.sayHello(), equalTo( "Hello Zest!" ) ); + } + + + private static final String APPLICATION = + "{\n" + + " \"name\": \"Build from JSON test.\",\n" + + " \"layers\": [\n" + + " {\n" + + " \"name\": \"layer1\",\n" + + " \"uses\": [ \"layer2\", \"layer3\"]\n" + + " },\n" + + " {\n" + + " \"name\": \"layer2\"\n" + + " },\n" + + " {\n" + + " \"name\": \"layer3\",\n" + + " \"modules\" : [\n" + + " {\n" + + " \"name\" : \"test module\",\n" + + " \"assemblers\" : [\n" + + " \"org.qi4j.bootstrap.builder.ApplicationBuilderTest$TestServiceAssembler\"\n" + + " ]\n" + + " }\n" + + " ]\n" + + " }\n" + + " ]\n" + + "}"; + + public static class TestServiceAssembler + implements Assembler + { + @Override + public void assemble( ModuleAssembly module ) + throws AssemblyException + { + module.addServices( TestService.class ); + } + } + + @Mixins( TestService.TestMixin.class ) + public interface TestService + { + String sayHello(); + + class TestMixin + implements TestService + { + + @Override + public String sayHello() + { + return "Hello Zest!"; + } + } + } +} http://git-wip-us.apache.org/repos/asf/polygene-java/blob/a789141d/core/bootstrap/src/test/java/org/qi4j/bootstrap/somepackage/Test2Value.java ---------------------------------------------------------------------- diff --git a/core/bootstrap/src/test/java/org/qi4j/bootstrap/somepackage/Test2Value.java b/core/bootstrap/src/test/java/org/qi4j/bootstrap/somepackage/Test2Value.java new file mode 100644 index 0000000..5b0d6c9 --- /dev/null +++ b/core/bootstrap/src/test/java/org/qi4j/bootstrap/somepackage/Test2Value.java @@ -0,0 +1,28 @@ +/* + * 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.qi4j.bootstrap.somepackage; + +import org.qi4j.api.value.ValueComposite; + +/** + */ +public interface Test2Value + extends ValueComposite +{ +} http://git-wip-us.apache.org/repos/asf/polygene-java/blob/a789141d/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 7126ff1..0000000 --- a/core/functional/src/main/java/org/apache/zest/functional/ForEach.java +++ /dev/null @@ -1,95 +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; - -/** - * 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( Specification<? 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/polygene-java/blob/a789141d/core/functional/src/main/java/org/apache/zest/functional/Function.java ---------------------------------------------------------------------- diff --git a/core/functional/src/main/java/org/apache/zest/functional/Function.java b/core/functional/src/main/java/org/apache/zest/functional/Function.java deleted file mode 100644 index 8107787..0000000 --- a/core/functional/src/main/java/org/apache/zest/functional/Function.java +++ /dev/null @@ -1,35 +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; - -/** - * Generic function interface to map from one type to another. - * - * This can be used with the Iterables methods to transform lists of objects. - * - * @param <From> - * @param <To> - */ -public interface Function<From, To> -{ - /** - * Map a single item from one type to another - * - * @param from the input item - * - * @return the mapped item - */ - To map( From from ); -} http://git-wip-us.apache.org/repos/asf/polygene-java/blob/a789141d/core/functional/src/main/java/org/apache/zest/functional/Function2.java ---------------------------------------------------------------------- diff --git a/core/functional/src/main/java/org/apache/zest/functional/Function2.java b/core/functional/src/main/java/org/apache/zest/functional/Function2.java deleted file mode 100644 index 1cc01c3..0000000 --- a/core/functional/src/main/java/org/apache/zest/functional/Function2.java +++ /dev/null @@ -1,33 +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; - -/** - * Generic function interface to map from two parameters to a third. - * - * This can be used with the Iterables methods to transform lists of objects. - */ -public interface Function2<First, Second, To> -{ - /** - * Map a single item from one type to another - * - * @param first - * @param second - * - * @return the mapped item - */ - To map( First first, Second second ); -} http://git-wip-us.apache.org/repos/asf/polygene-java/blob/a789141d/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 be72310..0000000 --- a/core/functional/src/main/java/org/apache/zest/functional/Functions.java +++ /dev/null @@ -1,276 +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; - -/** - * Utility functions. Combine these with methods in Iterables, for example. See FunctionsTest for usages. - */ -public final class Functions -{ - public static <A, B, C> Function2<Function<? super B, C>, Function<A, B>, Function<A, C>> compose() - { - return new Function2<Function<? super B, C>, Function<A, B>, Function<A, C>>() - { - @Override - public Function<A, C> map( Function<? super B, C> bcFunction, Function<A, B> abFunction ) - { - return compose( bcFunction, abFunction ); - } - }; - } - - /** - * 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 new Function<FROM, TO>() - { - @Override - public TO map( FROM from ) - { - return outer.map( inner.map( from ) ); - } - }; - } - - public static <TO, FROM extends TO> Function<FROM, TO> identity() - { - return new Function<FROM, TO>() - { - @Override - public TO map( FROM from ) - { - return from; - } - }; - } - - public static <FROM, TO> Function<FROM, TO> fromMap( final Map<FROM, TO> map ) - { - return new Function<FROM, TO>() - { - @Override - public TO map( FROM from ) - { - return map.get( from ); - } - }; - } - - public static <T> Function<T, T> withDefault( final T defaultValue ) - { - return new Function<T, T>() - { - @Override - public T map( T from ) - { - if( from == null ) - { - return defaultValue; - } - else - { - return from; - } - } - }; - } - - public static Function<Number, Long> longSum() - { - return new Function<Number, Long>() - { - long sum; - - @Override - public Long map( Number number ) - { - sum += number.longValue(); - return sum; - } - }; - } - - public static Function<Number, Integer> intSum() - { - return new Function<Number, Integer>() - { - int sum; - - @Override - public Integer map( Number number ) - { - sum += number.intValue(); - return sum; - } - }; - } - - /** - * Count the number of items in an iterable that matches a given specification. - * - * Sample usage: last( map( 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 Specification<T> specification ) - { - return new Function<T, Integer>() - { - int count; - - @Override - public Integer map( T item ) - { - if( specification.satisfiedBy( 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 Specification<T> specification ) - { - return new Function<T, Integer>() - { - int index = -1; - int current = 0; - - @Override - public Integer map( T item ) - { - if( index == -1 && specification.satisfiedBy( 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 Specification<T> specification, final Function<T, T> function ) - { - return new Function<T, T>() - { - @Override - public T map( T from ) - { - return specification.satisfiedBy( from ) ? function.map( 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.map( o1 ); - compareKeys.put( o1, key1 ); - } - - Comparable key2 = compareKeys.get( o2 ); - if( key2 == null ) - { - key2 = comparableFunction.map( o2 ); - compareKeys.put( o2, key2 ); - } - - return key1.compareTo( key2 ); - } - }; - } - - private Functions() - { - } -} http://git-wip-us.apache.org/repos/asf/polygene-java/blob/a789141d/core/functional/src/main/java/org/apache/zest/functional/HierarchicalVisitor.java ---------------------------------------------------------------------- diff --git a/core/functional/src/main/java/org/apache/zest/functional/HierarchicalVisitor.java b/core/functional/src/main/java/org/apache/zest/functional/HierarchicalVisitor.java deleted file mode 100644 index 6b5913b..0000000 --- a/core/functional/src/main/java/org/apache/zest/functional/HierarchicalVisitor.java +++ /dev/null @@ -1,53 +0,0 @@ -/* - * Copyright (c) 2012, Niclas Hedhman. 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; - -/** - * Visitor to visit hierarchies. - */ -public interface HierarchicalVisitor<NODE, LEAF, ThrowableType extends Throwable> extends Visitor<LEAF, ThrowableType> -{ - /** - * Enter an instance of T - * - * @param visited the visited instance which is now entered - * - * @return true if the visitor pattern should continue, false if it should be aborted for this level - * - * @throws ThrowableType if an exception occurred during processing. Any client call that initiated the visiting should - * get the exception in order to handle it properly. - */ - boolean visitEnter( NODE visited ) - throws ThrowableType; - - /** - * Leave an instance of T - * - * @param visited the visited instance which is now left - * - * @return true if the visitor pattern should continue, false if it should be aborted for the level of this node - * - * @throws ThrowableType if an exception occurred during processing. Any client call that initiated the visiting should - * get the exception in order to handle it properly. - */ - boolean visitLeave( NODE visited ) - throws ThrowableType; - - @Override - boolean visit( LEAF visited ) - throws ThrowableType; -} http://git-wip-us.apache.org/repos/asf/polygene-java/blob/a789141d/core/functional/src/main/java/org/apache/zest/functional/HierarchicalVisitorAdapter.java ---------------------------------------------------------------------- diff --git a/core/functional/src/main/java/org/apache/zest/functional/HierarchicalVisitorAdapter.java b/core/functional/src/main/java/org/apache/zest/functional/HierarchicalVisitorAdapter.java deleted file mode 100644 index 2c4ef80..0000000 --- a/core/functional/src/main/java/org/apache/zest/functional/HierarchicalVisitorAdapter.java +++ /dev/null @@ -1,47 +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; - -/** - * Generic Hierarchical Visitor interface. - */ -public class HierarchicalVisitorAdapter<NODE, LEAF, ThrowableType extends Throwable> - implements HierarchicalVisitor<NODE, LEAF, ThrowableType> -{ - @Override - public boolean visitEnter( NODE visited ) - throws ThrowableType - { - return true; - } - - @Override - public boolean visitLeave( NODE visited ) - throws ThrowableType - { - return true; - } - - @Override - public boolean visit( LEAF visited ) - throws ThrowableType - { - return true; - } -} http://git-wip-us.apache.org/repos/asf/polygene-java/blob/a789141d/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 deleted file mode 100644 index a77a519..0000000 --- a/core/functional/src/main/java/org/apache/zest/functional/Iterables.java +++ /dev/null @@ -1,939 +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.lang.reflect.Array; -import java.text.MessageFormat; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.Collection; -import java.util.Collections; -import java.util.Comparator; -import java.util.Enumeration; -import java.util.HashSet; -import java.util.Iterator; -import java.util.List; -import java.util.NoSuchElementException; -import java.util.Set; - -/** - * Utility methods for working with Iterables. See test for examples of how to use. - */ -public final class Iterables -{ - @SuppressWarnings( "raw" ) - private static final Iterable EMPTY = new Iterable() - { - Iterator iterator = new Iterator() - { - @Override - public boolean hasNext() - { - return false; - } - - @Override - public Object next() - { - throw new NoSuchElementException(); - } - - @Override - public void remove() - { - } - }; - - @Override - public Iterator iterator() - { - return iterator; - } - }; - - @SuppressWarnings( "unchecked" ) - public static <T> Iterable<T> empty() - { - 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>() - { - @Override - public Iterator<T> iterator() - { - final Iterator<T> iterator = iterable.iterator(); - - return new Iterator<T>() - { - int count; - - @Override - public boolean hasNext() - { - return count < limitItems && iterator.hasNext(); - } - - @Override - public T next() - { - count++; - return iterator.next(); - } - - @Override - public void remove() - { - iterator.remove(); - } - }; - } - }; - } - - public static <T> Iterable<T> unique( final Iterable<T> iterable ) - { - return new Iterable<T>() - { - @Override - public Iterator<T> iterator() - { - final Iterator<T> iterator = iterable.iterator(); - - return new Iterator<T>() - { - private final Set<T> items = new HashSet<>(); - private T nextItem; - - @Override - public boolean hasNext() - { - while( iterator.hasNext() ) - { - nextItem = iterator.next(); - if( items.add( nextItem ) ) - { - return true; - } - } - - return false; - } - - @Override - public T next() - { - if( nextItem == null && !hasNext() ) - { - throw new NoSuchElementException(); - } - - return nextItem; - } - - @Override - public void remove() - { - } - }; - } - }; - } - - public static <T, C extends Collection<T>> C addAll( C collection, Iterable<? extends T> iterable ) - { - for( T item : iterable ) - { - collection.add( item ); - } - return collection; - } - - public static long count( Iterable<?> iterable ) - { - long c = 0; - for( Object anIterable : iterable ) - { - c++; - } - return c; - } - - @SuppressWarnings( "unchecked" ) - public static <X> Iterable<X> filter( Specification<? /* super X*/> specification, Iterable<X> i ) - { - return new FilterIterable<>( i, (Specification<? super X>) specification ); - } - - public static <X> X first( Iterable<X> i ) - { - Iterator<X> iter = i.iterator(); - if( iter.hasNext() ) - { - return iter.next(); - } - else - { - return null; - } - } - - public static <X> X single( Iterable<X> i ) - { - Iterator<X> iter = i.iterator(); - if( iter.hasNext() ) - { - X result = iter.next(); - - if( iter.hasNext() ) - { - throw new IllegalArgumentException( "More than one element in iterable" ); - } - - return result; - } - else - { - throw new IllegalArgumentException( "No elements in iterable" ); - } - } - - public static <X> Iterable<X> skip( final int skip, final Iterable<X> iterable ) - { - return new Iterable<X>() - { - @Override - public Iterator<X> iterator() - { - Iterator<X> iterator = iterable.iterator(); - - for( int i = 0; i < skip; i++ ) - { - if( iterator.hasNext() ) - { - iterator.next(); - } - else - { - return Iterables.<X>empty().iterator(); - } - } - - return iterator; - } - }; - } - - public static <X> X last( Iterable<X> i ) - { - Iterator<X> iter = i.iterator(); - X item = null; - while( iter.hasNext() ) - { - item = iter.next(); - } - - return item; - } - - public static <X> Iterable<X> reverse( Iterable<X> iterable ) - { - List<X> list = toList( iterable ); - Collections.reverse( list ); - return list; - } - - public static <T> boolean matchesAny( Specification<? super T> specification, Iterable<T> iterable ) - { - boolean result = false; - - for( T item : iterable ) - { - if( ( (Specification<? super T>) specification ).satisfiedBy( item ) ) - { - result = true; - break; - } - } - - return result; - } - - public static <T> boolean matchesAll( Specification<? super T> specification, Iterable<T> iterable ) - { - boolean result = true; - for( T item : iterable ) - { - if( !specification.satisfiedBy( item ) ) - { - result = false; - } - } - - return result; - } - - public static <X> Iterable<X> flatten( Iterable<?>... multiIterator ) - { - return new FlattenIterable<>( Iterables.<Iterable<X>>cast( Arrays.asList( multiIterator ) ) ); - } - - public static <X, I extends Iterable<? extends X>> Iterable<X> flattenIterables( Iterable<I> multiIterator ) - // public static <X> Iterable<X> flattenIterables( Iterable<Iterable<?>> multiIterator ) - { - return new FlattenIterable<>( Iterables.<Iterable<X>>cast( multiIterator ) ); - } - - @SafeVarargs - public static <T> Iterable<T> mix( final Iterable<T>... iterables ) - { - return new Iterable<T>() - { - @Override - public Iterator<T> iterator() - { - final Iterable<Iterator<T>> iterators = toList( map( new Function<Iterable<T>, Iterator<T>>() - { - @Override - public Iterator<T> map( Iterable<T> iterable ) - { - return iterable.iterator(); - } - }, Iterables.iterable( iterables ) ) ); - - return new Iterator<T>() - { - Iterator<Iterator<T>> iterator; - - Iterator<T> iter; - - @Override - public boolean hasNext() - { - for( Iterator<T> iterator : iterators ) - { - if( iterator.hasNext() ) - { - return true; - } - } - - return false; - } - - @Override - public T next() - { - if( iterator == null ) - { - iterator = iterators.iterator(); - } - - while( iterator.hasNext() ) - { - iter = iterator.next(); - - if( iter.hasNext() ) - { - return iter.next(); - } - } - - iterator = null; - - return next(); - } - - @Override - public void remove() - { - if( iter != null ) - { - iter.remove(); - } - } - }; - } - }; - } - - @SuppressWarnings( "unchecked" ) - public static <FROM, TO> Iterable<TO> map( Function<? /* super FROM */, TO> function, Iterable<FROM> from ) - { - return new MapIterable<>( from, (Function<FROM, TO>) function ); - } - - public static <T> Iterable<T> iterable( Enumeration<T> enumeration ) - { - List<T> list = new ArrayList<>(); - while( enumeration.hasMoreElements() ) - { - T item = enumeration.nextElement(); - list.add( item ); - } - - return list; - } - - @SafeVarargs - public static <T> Iterable<T> iterable( T... items ) - { - return Arrays.asList( items ); - } - - @SuppressWarnings( {"raw", "unchecked"} ) - public static <T> Iterable<T> cast( Iterable<?> iterable ) - { - Iterable iter = iterable; - return iter; - } - - public static <FROM, TO> Function<FROM, TO> cast() - { - return new Function<FROM, TO>() - { - @Override - @SuppressWarnings( "unchecked" ) - public TO map( FROM from ) - { - return (TO) from; - } - }; - } - - public static <FROM, TO> TO fold( Function<? super FROM, TO> function, Iterable<? extends FROM> i ) - { - return last( map( function, i ) ); - } - - public static <T> Iterable<T> prepend( final T item, final Iterable<T> iterable ) - { - return new Iterable<T>() - { - @Override - public Iterator<T> iterator() - { - return new Iterator<T>() - { - T first = item; - Iterator<T> iterator; - - @Override - public boolean hasNext() - { - if( first != null ) - { - return true; - } - else - { - if( iterator == null ) - { - iterator = iterable.iterator(); - } - } - - return iterator.hasNext(); - } - - @Override - public T next() - { - if( first != null ) - { - try - { - return first; - } - finally - { - first = null; - } - } - else - { - return iterator.next(); - } - } - - @Override - public void remove() - { - } - }; - } - }; - } - - public static <T> Iterable<T> append( final T item, final Iterable<T> iterable ) - { - return new Iterable<T>() - { - @Override - public Iterator<T> iterator() - { - final Iterator<T> iterator = iterable.iterator(); - - return new Iterator<T>() - { - T last = item; - - @Override - public boolean hasNext() - { - if( iterator.hasNext() ) - { - return true; - } - else - { - return last != null; - } - } - - @Override - public T next() - { - if( iterator.hasNext() ) - { - return iterator.next(); - } - else - { - try - { - return last; - } - finally - { - last = null; - } - } - } - - @Override - public void remove() - { - } - }; - } - }; - } - - @SafeVarargs - public static <T> Iterable<T> debug( String format, - final Iterable<T> iterable, - final Function<T, String>... functions - ) - { - final MessageFormat msgFormat = new MessageFormat( format ); - - return map( new Function<T, T>() - { - @Override - public T map( T t ) - { - if( functions.length != 0 ) - { - String[] mapped = new String[ functions.length ]; - for( int i = 0; i < functions.length; i++ ) - { - Function<T, String> function = functions[i]; - mapped[i] = function.map( t ); - } - } - return t; - } - }, iterable ); - } - - public static <T> Iterable<T> cache( Iterable<T> iterable ) - { - return new CacheIterable<>( iterable ); - } - - public static <T> String toString( Iterable<T> iterable ) - { - return toString( iterable, new Function<T, String>() - { - @Override - public String map( T t ) - { - return t == null ? "[null]" : t.toString(); - } - }, "," ); - } - - public static <T> String toString( Iterable<T> iterable, Function<T, String> toStringFunction, String separator ) - { - StringBuilder builder = new StringBuilder(); - boolean first = true; - for( T item : iterable ) - { - if( !first ) - { - builder.append( separator ); - } - builder.append( toStringFunction.map( item ) ); - first = false; - } - return builder.toString(); - } - - public static <T> List<T> toList( Iterable<T> iterable ) - { - return addAll( new ArrayList<T>(), iterable ); - } - - public static Object[] toArray( Iterable<Object> iterable ) - { - return toArray( Object.class, iterable ); - } - - @SuppressWarnings( "unchecked" ) - public static <T> T[] toArray( Class<T> componentType, Iterable<T> iterable ) - { - if( iterable == null ) - { - return null; - } - List<T> list = toList( iterable ); - return list.toArray( (T[]) Array.newInstance( componentType, list.size() ) ); - } - - @SuppressWarnings( {"raw", "unchecked"} ) - public static <X extends Comparable> Iterable<X> sort( Iterable<X> iterable ) - { - List<X> list = toList( iterable ); - Collections.sort( list ); - return list; - } - - public static <X> Iterable<X> sort( Comparator<? super X> comparator, Iterable<X> iterable ) - { - List<X> list = toList( iterable ); - Collections.sort( list, comparator ); - return list; - } - - private static class MapIterable<FROM, TO> - implements Iterable<TO> - { - private final Iterable<FROM> from; - private final Function<? super FROM, TO> function; - - private MapIterable( Iterable<FROM> from, Function<? super FROM, TO> function ) - { - this.from = from; - this.function = function; - } - - @Override - public Iterator<TO> iterator() - { - return new MapIterator<>( from.iterator(), function ); - } - - static class MapIterator<FROM, TO> - implements Iterator<TO> - { - private final Iterator<FROM> fromIterator; - private final Function<? super FROM, TO> function; - - private MapIterator( Iterator<FROM> fromIterator, Function<? super FROM, TO> function ) - { - this.fromIterator = fromIterator; - this.function = function; - } - - @Override - public boolean hasNext() - { - return fromIterator.hasNext(); - } - - @Override - public TO next() - { - FROM from = fromIterator.next(); - return function.map( from ); - } - - @Override - public void remove() - { - fromIterator.remove(); - } - } - - } - - private static class FilterIterable<T> - implements Iterable<T> - { - private final Iterable<T> iterable; - - private final Specification<? super T> specification; - - private FilterIterable( Iterable<T> iterable, Specification<? super T> specification ) - { - this.iterable = iterable; - this.specification = specification; - } - - @Override - public Iterator<T> iterator() - { - return new FilterIterator<>( iterable.iterator(), specification ); - } - - private static class FilterIterator<T> - implements Iterator<T> - { - private final Iterator<T> iterator; - - private final Specification<? super T> specification; - - private T currentValue; - boolean finished = false; - boolean nextConsumed = true; - - private FilterIterator( Iterator<T> iterator, Specification<? super T> specification ) - { - this.specification = specification; - this.iterator = iterator; - } - - public boolean moveToNextValid() - { - boolean found = false; - while( !found && iterator.hasNext() ) - { - T currentValue = iterator.next(); - boolean satisfies = specification.satisfiedBy( currentValue ); - - if( satisfies ) - { - found = true; - this.currentValue = currentValue; - nextConsumed = false; - } - } - if( !found ) - { - finished = true; - } - return found; - } - - @Override - public T next() - { - if( !nextConsumed ) - { - nextConsumed = true; - return currentValue; - } - else - { - if( !finished ) - { - if( moveToNextValid() ) - { - nextConsumed = true; - return currentValue; - } - } - } - return null; - } - - @Override - public boolean hasNext() - { - return !finished - && ( !nextConsumed || moveToNextValid() ); - } - - @Override - public void remove() - { - } - } - - } - - private static class FlattenIterable<T, I extends Iterable<? extends T>> - implements Iterable<T> - { - private final Iterable<I> iterable; - - private FlattenIterable( Iterable<I> iterable ) - { - this.iterable = iterable; - } - - @Override - public Iterator<T> iterator() - { - return new FlattenIterator<>( iterable.iterator() ); - } - - static class FlattenIterator<T, I extends Iterable<? extends T>> - implements Iterator<T> - { - private final Iterator<I> iterator; - private Iterator<? extends T> currentIterator; - - private FlattenIterator( Iterator<I> iterator ) - { - this.iterator = iterator; - currentIterator = null; - } - - @Override - public boolean hasNext() - { - if( currentIterator == null ) - { - if( iterator.hasNext() ) - { - I next = iterator.next(); - currentIterator = next.iterator(); - } - else - { - return false; - } - } - - while( !currentIterator.hasNext() - && iterator.hasNext() ) - { - currentIterator = iterator.next().iterator(); - } - - return currentIterator.hasNext(); - } - - @Override - public T next() - { - return currentIterator.next(); - } - - @Override - public void remove() - { - if( currentIterator == null ) - { - throw new IllegalStateException(); - } - - currentIterator.remove(); - } - } - - } - - private static class CacheIterable<T> - implements Iterable<T> - { - private final Iterable<T> iterable; - private Iterable<T> cache; - - private CacheIterable( Iterable<T> iterable ) - { - this.iterable = iterable; - } - - @Override - public Iterator<T> iterator() - { - if( cache != null ) - { - return cache.iterator(); - } - - final Iterator<T> source = iterable.iterator(); - - return new Iterator<T>() - { - List<T> iteratorCache = new ArrayList<>(); - - @Override - public boolean hasNext() - { - boolean hasNext = source.hasNext(); - if( !hasNext ) - { - cache = iteratorCache; - } - return hasNext; - } - - @Override - public T next() - { - T next = source.next(); - iteratorCache.add( next ); - return next; - } - - @Override - public void remove() - { - - } - }; - } - } - - private Iterables() - { - } - -} http://git-wip-us.apache.org/repos/asf/polygene-java/blob/a789141d/core/functional/src/main/java/org/apache/zest/functional/Specification.java ---------------------------------------------------------------------- diff --git a/core/functional/src/main/java/org/apache/zest/functional/Specification.java b/core/functional/src/main/java/org/apache/zest/functional/Specification.java deleted file mode 100644 index 3ff725d..0000000 --- a/core/functional/src/main/java/org/apache/zest/functional/Specification.java +++ /dev/null @@ -1,37 +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; - -/** - * Generic specification interface. - * - * @param <T> - */ -// START SNIPPET: specification -public interface Specification<T> -{ -// END SNIPPET: specification - - /** - * Test whether an item matches the given specification - * - * @param item the item to be tested - * - * @return true if the item matches, false otherwise - */ -// START SNIPPET: specification - boolean satisfiedBy( T item ); -} -// END SNIPPET: specification http://git-wip-us.apache.org/repos/asf/polygene-java/blob/a789141d/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 6da1739..0000000 --- a/core/functional/src/main/java/org/apache/zest/functional/Specifications.java +++ /dev/null @@ -1,206 +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; - -/** - * Common generic specification expressions - */ -public class Specifications -{ - public static <T> Specification<T> TRUE() - { - return new Specification<T>() - { - @Override - public boolean satisfiedBy( T instance ) - { - return true; - } - }; - } - - public static <T> Specification<T> not( final Specification<T> specification ) - { - return new Specification<T>() - { - @Override - public boolean satisfiedBy( T instance ) - { - return !specification.satisfiedBy( instance ); - } - }; - } - - @SafeVarargs - public static <T> AndSpecification<T> and( final Specification<T>... specifications ) - { - return and( Iterables.iterable( specifications ) ); - } - - public static <T> AndSpecification<T> and( final Iterable<Specification<T>> specifications ) - { - return new AndSpecification<>( specifications ); - } - - @SafeVarargs - public static <T> OrSpecification<T> or( final Specification<T>... specifications ) - { - return or( Iterables.iterable( specifications ) ); - } - - public static <T> OrSpecification<T> or( final Iterable<Specification<T>> specifications ) - { - return new OrSpecification<>( specifications ); - } - - @SafeVarargs - public static <T> Specification<T> in( final T... allowed ) - { - return in( Iterables.iterable( allowed ) ); - } - - public static <T> Specification<T> in( final Iterable<T> allowed ) - { - return new Specification<T>() - { - @Override - public boolean satisfiedBy( T item ) - { - for( T allow : allowed ) - { - if( allow.equals( item ) ) - { - return true; - } - } - return false; - } - }; - } - - public static <T> Specification<T> notNull() - { - return new Specification<T>() - { - @Override - public boolean satisfiedBy( T item ) - { - return item != null; - } - }; - } - - public static <FROM, TO> Specification<FROM> translate( final Function<FROM, TO> function, - final Specification<? super TO> specification - ) - { - return new Specification<FROM>() - { - @Override - public boolean satisfiedBy( FROM item ) - { - return specification.satisfiedBy( function.map( item ) ); - } - }; - } - - /** - * AND Specification. - */ - public static class AndSpecification<T> - implements Specification<T> - { - private final Iterable<Specification<T>> specifications; - - private AndSpecification( Iterable<Specification<T>> specifications ) - { - this.specifications = specifications; - } - - @Override - public boolean satisfiedBy( T instance ) - { - for( Specification<T> specification : specifications ) - { - if( !specification.satisfiedBy( instance ) ) - { - return false; - } - } - - return true; - } - - @SafeVarargs - public final AndSpecification<T> and( Specification<T>... specifications ) - { - Iterable<Specification<T>> iterable = Iterables.iterable( specifications ); - Iterable<Specification<T>> flatten = Iterables.flatten( this.specifications, iterable ); - return Specifications.and( flatten ); - } - - @SafeVarargs - public final OrSpecification<T> or( Specification<T>... specifications ) - { - return Specifications.or( Iterables.prepend( this, Iterables.iterable( specifications ) ) ); - } - } - - /** - * OR Specification. - */ - public static class OrSpecification<T> - implements Specification<T> - { - private final Iterable<Specification<T>> specifications; - - private OrSpecification( Iterable<Specification<T>> specifications ) - { - this.specifications = specifications; - } - - @Override - public boolean satisfiedBy( T instance ) - { - for( Specification<T> specification : specifications ) - { - if( specification.satisfiedBy( instance ) ) - { - return true; - } - } - - return false; - } - - @SafeVarargs - public final AndSpecification<T> and( Specification<T>... specifications ) - { - return Specifications.and( Iterables.prepend( this, Iterables.iterable( specifications ) ) ); - } - - @SafeVarargs - public final OrSpecification<T> or( Specification<T>... specifications ) - { - Iterable<Specification<T>> iterable = Iterables.iterable( specifications ); - Iterable<Specification<T>> flatten = Iterables.flatten( this.specifications, iterable ); - return Specifications.or( flatten ); - } - } - - private Specifications() - { - } - -} http://git-wip-us.apache.org/repos/asf/polygene-java/blob/a789141d/core/functional/src/main/java/org/apache/zest/functional/Visitable.java ---------------------------------------------------------------------- diff --git a/core/functional/src/main/java/org/apache/zest/functional/Visitable.java b/core/functional/src/main/java/org/apache/zest/functional/Visitable.java deleted file mode 100644 index ee05807..0000000 --- a/core/functional/src/main/java/org/apache/zest/functional/Visitable.java +++ /dev/null @@ -1,28 +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; - -/** - * Interface that visitable objects should implement. - */ -public interface Visitable<T> -{ - <ThrowableType extends Throwable> boolean accept( Visitor<? super T, ThrowableType> visitor ) - throws ThrowableType; -} http://git-wip-us.apache.org/repos/asf/polygene-java/blob/a789141d/core/functional/src/main/java/org/apache/zest/functional/VisitableHierarchy.java ---------------------------------------------------------------------- diff --git a/core/functional/src/main/java/org/apache/zest/functional/VisitableHierarchy.java b/core/functional/src/main/java/org/apache/zest/functional/VisitableHierarchy.java deleted file mode 100644 index 60d99af..0000000 --- a/core/functional/src/main/java/org/apache/zest/functional/VisitableHierarchy.java +++ /dev/null @@ -1,28 +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; - -/** - * Interface that visitable hierarchies of objects should implement. - */ -public interface VisitableHierarchy<NODE, LEAF> -{ - <ThrowableType extends Throwable> boolean accept( HierarchicalVisitor<? super NODE, ? super LEAF, ThrowableType> visitor ) - throws ThrowableType; -} http://git-wip-us.apache.org/repos/asf/polygene-java/blob/a789141d/core/functional/src/main/java/org/apache/zest/functional/Visitor.java ---------------------------------------------------------------------- diff --git a/core/functional/src/main/java/org/apache/zest/functional/Visitor.java b/core/functional/src/main/java/org/apache/zest/functional/Visitor.java deleted file mode 100644 index 9a72a1f..0000000 --- a/core/functional/src/main/java/org/apache/zest/functional/Visitor.java +++ /dev/null @@ -1,38 +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; - -/** - * Generic Visitor interface. - */ -public interface Visitor<T, ThrowableType extends Throwable> -{ - /** - * Visit an instance of T - * - * @param visited the visited instance - * - * @return true if the visitor pattern should continue, false if it should be aborted - * - * @throws ThrowableType if an exception occurred during processing. Any client call that initiated the visiting should - * get the exception in order to handle it properly. - */ - boolean visit( T visited ) - throws ThrowableType; -} http://git-wip-us.apache.org/repos/asf/polygene-java/blob/a789141d/core/functional/src/main/java/org/apache/zest/functional/package.html ---------------------------------------------------------------------- diff --git a/core/functional/src/main/java/org/apache/zest/functional/package.html b/core/functional/src/main/java/org/apache/zest/functional/package.html deleted file mode 100644 index 920a9ef..0000000 --- a/core/functional/src/main/java/org/apache/zest/functional/package.html +++ /dev/null @@ -1,21 +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. ---> -<html> - <body> - <h2>Functional API.</h2> - </body> -</html> http://git-wip-us.apache.org/repos/asf/polygene-java/blob/a789141d/core/functional/src/main/java/org/qi4j/functional/ForEach.java ---------------------------------------------------------------------- diff --git a/core/functional/src/main/java/org/qi4j/functional/ForEach.java b/core/functional/src/main/java/org/qi4j/functional/ForEach.java new file mode 100644 index 0000000..c2bbe3e --- /dev/null +++ b/core/functional/src/main/java/org/qi4j/functional/ForEach.java @@ -0,0 +1,95 @@ +/* + * 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.qi4j.functional; + +import java.util.Iterator; + +/** + * 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( Specification<? 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/polygene-java/blob/a789141d/core/functional/src/main/java/org/qi4j/functional/Function.java ---------------------------------------------------------------------- diff --git a/core/functional/src/main/java/org/qi4j/functional/Function.java b/core/functional/src/main/java/org/qi4j/functional/Function.java new file mode 100644 index 0000000..3edd3f1 --- /dev/null +++ b/core/functional/src/main/java/org/qi4j/functional/Function.java @@ -0,0 +1,35 @@ +/* + * 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.qi4j.functional; + +/** + * Generic function interface to map from one type to another. + * + * This can be used with the Iterables methods to transform lists of objects. + * + * @param <From> + * @param <To> + */ +public interface Function<From, To> +{ + /** + * Map a single item from one type to another + * + * @param from the input item + * + * @return the mapped item + */ + To map( From from ); +} http://git-wip-us.apache.org/repos/asf/polygene-java/blob/a789141d/core/functional/src/main/java/org/qi4j/functional/Function2.java ---------------------------------------------------------------------- diff --git a/core/functional/src/main/java/org/qi4j/functional/Function2.java b/core/functional/src/main/java/org/qi4j/functional/Function2.java new file mode 100644 index 0000000..7803965 --- /dev/null +++ b/core/functional/src/main/java/org/qi4j/functional/Function2.java @@ -0,0 +1,33 @@ +/* + * 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.qi4j.functional; + +/** + * Generic function interface to map from two parameters to a third. + * + * This can be used with the Iterables methods to transform lists of objects. + */ +public interface Function2<First, Second, To> +{ + /** + * Map a single item from one type to another + * + * @param first + * @param second + * + * @return the mapped item + */ + To map( First first, Second second ); +}