http://git-wip-us.apache.org/repos/asf/ignite/blob/a3f845b0/modules/core/src/main/java/org/apache/ignite/internal/util/lang/gridfunc/IsAllPredicate.java ---------------------------------------------------------------------- diff --git a/modules/core/src/main/java/org/apache/ignite/internal/util/lang/gridfunc/IsAllPredicate.java b/modules/core/src/main/java/org/apache/ignite/internal/util/lang/gridfunc/IsAllPredicate.java new file mode 100644 index 0000000..e829bef --- /dev/null +++ b/modules/core/src/main/java/org/apache/ignite/internal/util/lang/gridfunc/IsAllPredicate.java @@ -0,0 +1,52 @@ +/* + * 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.ignite.internal.util.lang.gridfunc; + +import org.apache.ignite.internal.util.lang.GridFunc; +import org.apache.ignite.internal.util.typedef.internal.S; +import org.apache.ignite.lang.IgnitePredicate; + +/** + * Predicate that evaluates to {@code true} if each of its component preds evaluates to {@code true}. + * + * @param <T> Type of the free variable, i.e. the element the predicate is called on. + */ +public class IsAllPredicate<T> implements IgnitePredicate<T> { + /** */ + private static final long serialVersionUID = 0L; + + /** */ + private final IgnitePredicate<? super T>[] preds; + + /** + * @param preds Passed in predicate. If none provided - always-{@code false} predicate is returned. + */ + public IsAllPredicate(IgnitePredicate<? super T>... preds) { + this.preds = preds; + } + + /** {@inheritDoc} */ + @Override public boolean apply(T t) { + return GridFunc.isAll(t, preds); + } + + /** {@inheritDoc} */ + @Override public String toString() { + return S.toString(IsAllPredicate.class, this); + } +}
http://git-wip-us.apache.org/repos/asf/ignite/blob/a3f845b0/modules/core/src/main/java/org/apache/ignite/internal/util/lang/gridfunc/IsAssignableFromPredicate.java ---------------------------------------------------------------------- diff --git a/modules/core/src/main/java/org/apache/ignite/internal/util/lang/gridfunc/IsAssignableFromPredicate.java b/modules/core/src/main/java/org/apache/ignite/internal/util/lang/gridfunc/IsAssignableFromPredicate.java new file mode 100644 index 0000000..51838e4 --- /dev/null +++ b/modules/core/src/main/java/org/apache/ignite/internal/util/lang/gridfunc/IsAssignableFromPredicate.java @@ -0,0 +1,51 @@ +/* + * 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.ignite.internal.util.lang.gridfunc; + +import org.apache.ignite.internal.util.typedef.P1; +import org.apache.ignite.internal.util.typedef.internal.S; + +/** + * Predicate that evaluates to {@code true} if its free variable is instance of the given class. + * + * @param <T> Type of the free variable, i.e. the element the predicate is called on. + */ +public class IsAssignableFromPredicate<T> implements P1<T> { + /** */ + private static final long serialVersionUID = 0L; + + /** */ + private final Class<?> cls; + + /** + * @param cls Class to compare to. + */ + public IsAssignableFromPredicate(Class<?> cls) { + this.cls = cls; + } + + /** {@inheritDoc} */ + @Override public boolean apply(T t) { + return t != null && cls.isAssignableFrom(t.getClass()); + } + + /** {@inheritDoc} */ + @Override public String toString() { + return S.toString(IsAssignableFromPredicate.class, this); + } +} http://git-wip-us.apache.org/repos/asf/ignite/blob/a3f845b0/modules/core/src/main/java/org/apache/ignite/internal/util/lang/gridfunc/IsNotAllPredicate.java ---------------------------------------------------------------------- diff --git a/modules/core/src/main/java/org/apache/ignite/internal/util/lang/gridfunc/IsNotAllPredicate.java b/modules/core/src/main/java/org/apache/ignite/internal/util/lang/gridfunc/IsNotAllPredicate.java new file mode 100644 index 0000000..ebcb8a9 --- /dev/null +++ b/modules/core/src/main/java/org/apache/ignite/internal/util/lang/gridfunc/IsNotAllPredicate.java @@ -0,0 +1,52 @@ +/* + * 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.ignite.internal.util.lang.gridfunc; + +import org.apache.ignite.internal.util.lang.GridFunc; +import org.apache.ignite.internal.util.typedef.internal.S; +import org.apache.ignite.lang.IgnitePredicate; + +/** + * Negated predicate. + * + * @param <T> Type of the free variable, i.e. the element the predicate is called on. + */ +public class IsNotAllPredicate<T> implements IgnitePredicate<T> { + /** */ + private static final long serialVersionUID = 0L; + + /** */ + private final IgnitePredicate<? super T>[] preds; + + /** + * @param preds Predicate to negate. + */ + public IsNotAllPredicate(IgnitePredicate<? super T>... preds) { + this.preds = preds; + } + + /** {@inheritDoc} */ + @Override public boolean apply(T t) { + return !GridFunc.isAll(t, preds); + } + + /** {@inheritDoc} */ + @Override public String toString() { + return S.toString(IsNotAllPredicate.class, this); + } +} http://git-wip-us.apache.org/repos/asf/ignite/blob/a3f845b0/modules/core/src/main/java/org/apache/ignite/internal/util/lang/gridfunc/IsNotDonePredicate.java ---------------------------------------------------------------------- diff --git a/modules/core/src/main/java/org/apache/ignite/internal/util/lang/gridfunc/IsNotDonePredicate.java b/modules/core/src/main/java/org/apache/ignite/internal/util/lang/gridfunc/IsNotDonePredicate.java new file mode 100644 index 0000000..4d9762d --- /dev/null +++ b/modules/core/src/main/java/org/apache/ignite/internal/util/lang/gridfunc/IsNotDonePredicate.java @@ -0,0 +1,40 @@ +/* + * 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.ignite.internal.util.lang.gridfunc; + +import org.apache.ignite.internal.IgniteInternalFuture; +import org.apache.ignite.internal.util.typedef.internal.S; +import org.apache.ignite.lang.IgnitePredicate; + +/** + * IgniteInternalFuture is not done predicate. + */ +public class IsNotDonePredicate implements IgnitePredicate<IgniteInternalFuture<?>> { + /** */ + private static final long serialVersionUID = 0L; + + /** {@inheritDoc} */ + @Override public boolean apply(IgniteInternalFuture<?> f) { + return !f.isDone(); + } + + /** {@inheritDoc} */ + @Override public String toString() { + return S.toString(IsNotDonePredicate.class, this); + } +} http://git-wip-us.apache.org/repos/asf/ignite/blob/a3f845b0/modules/core/src/main/java/org/apache/ignite/internal/util/lang/gridfunc/IsNotNullPredicate.java ---------------------------------------------------------------------- diff --git a/modules/core/src/main/java/org/apache/ignite/internal/util/lang/gridfunc/IsNotNullPredicate.java b/modules/core/src/main/java/org/apache/ignite/internal/util/lang/gridfunc/IsNotNullPredicate.java new file mode 100644 index 0000000..c67a015 --- /dev/null +++ b/modules/core/src/main/java/org/apache/ignite/internal/util/lang/gridfunc/IsNotNullPredicate.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.apache.ignite.internal.util.lang.gridfunc; + +import org.apache.ignite.internal.util.typedef.internal.S; +import org.apache.ignite.lang.IgnitePredicate; + +/** + * Defines a predicate which checks a parameter on <code>null</code>. + * + * @param <E> Type of predicate parameter. + */ +public class IsNotNullPredicate<E> implements IgnitePredicate<E> { + /** */ + private static final long serialVersionUID = 0L; + + /** + * @param e Parameter for check. + * @return 'true' if parameter NOT equals to <code>null</code>, otherwise 'false'. + */ + @Override public boolean apply(E e) { + return e != null; + } + + /** {@inheritDoc} */ + @Override public String toString() { + return S.toString(IsNotNullPredicate.class, this); + } +} http://git-wip-us.apache.org/repos/asf/ignite/blob/a3f845b0/modules/core/src/main/java/org/apache/ignite/internal/util/lang/gridfunc/IsNullPredicate.java ---------------------------------------------------------------------- diff --git a/modules/core/src/main/java/org/apache/ignite/internal/util/lang/gridfunc/IsNullPredicate.java b/modules/core/src/main/java/org/apache/ignite/internal/util/lang/gridfunc/IsNullPredicate.java new file mode 100644 index 0000000..7972928 --- /dev/null +++ b/modules/core/src/main/java/org/apache/ignite/internal/util/lang/gridfunc/IsNullPredicate.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.apache.ignite.internal.util.lang.gridfunc; + +import org.apache.ignite.internal.util.typedef.internal.S; +import org.apache.ignite.lang.IgnitePredicate; + +/** + * Defines a predicate which checks a parameter on <code>null</code>. + * + * @param <E> Type of predicate parameter. + */ +public class IsNullPredicate<E> implements IgnitePredicate<E> { + /** */ + private static final long serialVersionUID = 0L; + + /** + * @param e Parameter for check. + * @return 'true' if parameter equals to <code>null</code>, otherwise 'false'. + */ + @Override public boolean apply(E e) { + return e == null; + } + + /** {@inheritDoc} */ + @Override public String toString() { + return S.toString(IsNullPredicate.class, this); + } +} http://git-wip-us.apache.org/repos/asf/ignite/blob/a3f845b0/modules/core/src/main/java/org/apache/ignite/internal/util/lang/gridfunc/LinkedListFactoryCallable.java ---------------------------------------------------------------------- diff --git a/modules/core/src/main/java/org/apache/ignite/internal/util/lang/gridfunc/LinkedListFactoryCallable.java b/modules/core/src/main/java/org/apache/ignite/internal/util/lang/gridfunc/LinkedListFactoryCallable.java new file mode 100644 index 0000000..4bf9948 --- /dev/null +++ b/modules/core/src/main/java/org/apache/ignite/internal/util/lang/gridfunc/LinkedListFactoryCallable.java @@ -0,0 +1,40 @@ +/* + * 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.ignite.internal.util.lang.gridfunc; + +import java.util.LinkedList; +import org.apache.ignite.internal.util.typedef.internal.S; +import org.apache.ignite.lang.IgniteCallable; + +/** + * Linked list factory. + */ +public class LinkedListFactoryCallable implements IgniteCallable<LinkedList> { + /** */ + private static final long serialVersionUID = 0L; + + /** {@inheritDoc} */ + @Override public LinkedList call() { + return new LinkedList(); + } + + /** {@inheritDoc} */ + @Override public String toString() { + return S.toString(LinkedListFactoryCallable.class, this); + } +} http://git-wip-us.apache.org/repos/asf/ignite/blob/a3f845b0/modules/core/src/main/java/org/apache/ignite/internal/util/lang/gridfunc/LongSumReducer.java ---------------------------------------------------------------------- diff --git a/modules/core/src/main/java/org/apache/ignite/internal/util/lang/gridfunc/LongSumReducer.java b/modules/core/src/main/java/org/apache/ignite/internal/util/lang/gridfunc/LongSumReducer.java new file mode 100644 index 0000000..8b5d6dc --- /dev/null +++ b/modules/core/src/main/java/org/apache/ignite/internal/util/lang/gridfunc/LongSumReducer.java @@ -0,0 +1,51 @@ +/* + * 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.ignite.internal.util.lang.gridfunc; + +import java.util.concurrent.atomic.AtomicLong; +import org.apache.ignite.internal.util.typedef.internal.S; +import org.apache.ignite.lang.IgniteReducer; + +/** + * Reducer that calculates sum of long integer elements. + */ +public class LongSumReducer implements IgniteReducer<Long, Long> { + /** */ + private static final long serialVersionUID = 0L; + + /** */ + private AtomicLong sum = new AtomicLong(0); + + /** {@inheritDoc} */ + @Override public boolean collect(Long e) { + if (e != null) + sum.addAndGet(e); + + return true; + } + + /** {@inheritDoc} */ + @Override public Long reduce() { + return sum.get(); + } + + /** {@inheritDoc} */ + @Override public String toString() { + return S.toString(LongSumReducer.class, this); + } +} http://git-wip-us.apache.org/repos/asf/ignite/blob/a3f845b0/modules/core/src/main/java/org/apache/ignite/internal/util/lang/gridfunc/MapFactoryCallable.java ---------------------------------------------------------------------- diff --git a/modules/core/src/main/java/org/apache/ignite/internal/util/lang/gridfunc/MapFactoryCallable.java b/modules/core/src/main/java/org/apache/ignite/internal/util/lang/gridfunc/MapFactoryCallable.java new file mode 100644 index 0000000..ff59817 --- /dev/null +++ b/modules/core/src/main/java/org/apache/ignite/internal/util/lang/gridfunc/MapFactoryCallable.java @@ -0,0 +1,41 @@ +/* + * 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.ignite.internal.util.lang.gridfunc; + +import java.util.HashMap; +import java.util.Map; +import org.apache.ignite.internal.util.typedef.internal.S; +import org.apache.ignite.lang.IgniteCallable; + +/** + * Hash map factory. + */ +public class MapFactoryCallable implements IgniteCallable<Map> { + /** */ + private static final long serialVersionUID = 0L; + + /** {@inheritDoc} */ + @Override public Map call() { + return new HashMap(); + } + + /** {@inheritDoc} */ + @Override public String toString() { + return S.toString(MapFactoryCallable.class, this); + } +} http://git-wip-us.apache.org/repos/asf/ignite/blob/a3f845b0/modules/core/src/main/java/org/apache/ignite/internal/util/lang/gridfunc/NoOpClosure.java ---------------------------------------------------------------------- diff --git a/modules/core/src/main/java/org/apache/ignite/internal/util/lang/gridfunc/NoOpClosure.java b/modules/core/src/main/java/org/apache/ignite/internal/util/lang/gridfunc/NoOpClosure.java new file mode 100644 index 0000000..b687a4b --- /dev/null +++ b/modules/core/src/main/java/org/apache/ignite/internal/util/lang/gridfunc/NoOpClosure.java @@ -0,0 +1,39 @@ +/* + * 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.ignite.internal.util.lang.gridfunc; + +import org.apache.ignite.internal.util.lang.GridAbsClosure; +import org.apache.ignite.internal.util.typedef.internal.S; + +/** + * Absolute closure that does nothing. + */ +public class NoOpClosure extends GridAbsClosure { + /** */ + private static final long serialVersionUID = 0L; + + /** {@inheritDoc} */ + @Override public void apply() { + // No-op. + } + + /** {@inheritDoc} */ + @Override public String toString() { + return S.toString(NoOpClosure.class, this); + } +} http://git-wip-us.apache.org/repos/asf/ignite/blob/a3f845b0/modules/core/src/main/java/org/apache/ignite/internal/util/lang/gridfunc/NotContainsPredicate.java ---------------------------------------------------------------------- diff --git a/modules/core/src/main/java/org/apache/ignite/internal/util/lang/gridfunc/NotContainsPredicate.java b/modules/core/src/main/java/org/apache/ignite/internal/util/lang/gridfunc/NotContainsPredicate.java new file mode 100644 index 0000000..5f8ba9a --- /dev/null +++ b/modules/core/src/main/java/org/apache/ignite/internal/util/lang/gridfunc/NotContainsPredicate.java @@ -0,0 +1,54 @@ +/* + * 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.ignite.internal.util.lang.gridfunc; + +import java.util.Collection; +import org.apache.ignite.internal.util.typedef.internal.S; +import org.apache.ignite.lang.IgnitePredicate; + +/** + * Predicate that returns {@code true} if its free variable is not contained in given collection. + * + * @param <T> Type of the free variable for the predicate and type of the collection elements. + */ +public class NotContainsPredicate<T> implements IgnitePredicate<T> { + /** */ + private static final long serialVersionUID = 0L; + + /** */ + private final Collection<? extends T> col; + + /** + * @param col Collection to check for containment. + */ + public NotContainsPredicate(Collection<? extends T> col) { + this.col = col; + } + + /** {@inheritDoc} */ + @Override public boolean apply(T t) { + assert col != null; + + return !col.contains(t); + } + + /** {@inheritDoc} */ + public String toString() { + return S.toString(NotContainsPredicate.class, this); + } +} http://git-wip-us.apache.org/repos/asf/ignite/blob/a3f845b0/modules/core/src/main/java/org/apache/ignite/internal/util/lang/gridfunc/NotEqualPredicate.java ---------------------------------------------------------------------- diff --git a/modules/core/src/main/java/org/apache/ignite/internal/util/lang/gridfunc/NotEqualPredicate.java b/modules/core/src/main/java/org/apache/ignite/internal/util/lang/gridfunc/NotEqualPredicate.java new file mode 100644 index 0000000..c84e849 --- /dev/null +++ b/modules/core/src/main/java/org/apache/ignite/internal/util/lang/gridfunc/NotEqualPredicate.java @@ -0,0 +1,53 @@ +/* + * 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.ignite.internal.util.lang.gridfunc; + +import org.apache.ignite.internal.util.lang.GridFunc; +import org.apache.ignite.internal.util.typedef.internal.S; +import org.apache.ignite.lang.IgnitePredicate; + +/** + * Predicate that evaluates to {@code true} if its free variable is equal to {@code target} or both are + * {@code null}. + * + * @param <T> Type of the free variable, i.e. the element the predicate is called on. + */ +public class NotEqualPredicate<T> implements IgnitePredicate<T> { + /** */ + private static final long serialVersionUID = 0L; + + /** */ + private final T target; + + /** + * @param target Object to compare free variable to. + */ + public NotEqualPredicate(T target) { + this.target = target; + } + + /** {@inheritDoc} */ + @Override public boolean apply(T t) { + return !GridFunc.eq(t, target); + } + + /** {@inheritDoc} */ + @Override public String toString() { + return S.toString(NotEqualPredicate.class, this); + } +} http://git-wip-us.apache.org/repos/asf/ignite/blob/a3f845b0/modules/core/src/main/java/org/apache/ignite/internal/util/lang/gridfunc/PredicateCollectionView.java ---------------------------------------------------------------------- diff --git a/modules/core/src/main/java/org/apache/ignite/internal/util/lang/gridfunc/PredicateCollectionView.java b/modules/core/src/main/java/org/apache/ignite/internal/util/lang/gridfunc/PredicateCollectionView.java new file mode 100644 index 0000000..b4785a7 --- /dev/null +++ b/modules/core/src/main/java/org/apache/ignite/internal/util/lang/gridfunc/PredicateCollectionView.java @@ -0,0 +1,78 @@ +/* + * 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.ignite.internal.util.lang.gridfunc; + +import java.util.Collection; +import java.util.Iterator; +import org.apache.ignite.internal.util.GridSerializableCollection; +import org.apache.ignite.internal.util.lang.GridFunc; +import org.apache.ignite.internal.util.typedef.F; +import org.apache.ignite.internal.util.typedef.internal.S; +import org.apache.ignite.lang.IgnitePredicate; +import org.jetbrains.annotations.NotNull; + +/** + * Light-weight view on given col with provided predicate. + * + * @param <T> Type of the col. + */ +public class PredicateCollectionView<T> extends GridSerializableCollection<T> { + /** */ + private static final long serialVersionUID = 0L; + + /** */ + private final Collection<T> col; + + /** */ + private final IgnitePredicate<? super T>[] preds; + + /** + * @param col Input col that serves as a base for the view. + * @param preds Optional preds. If preds are not provided - all elements will be in the view. + */ + public PredicateCollectionView(Collection<T> col, IgnitePredicate<? super T>... preds) { + this.col = col; + this.preds = preds; + } + + /** {@inheritDoc} */ + @Override public boolean add(T e) { + // Pass through (will fail for readonly). + return GridFunc.isAll(e, preds) && col.add(e); + } + + /** {@inheritDoc} */ + @NotNull @Override public Iterator<T> iterator() { + return F.iterator0(col, false, preds); + } + + /** {@inheritDoc} */ + @Override public int size() { + return F.size(col, preds); + } + + /** {@inheritDoc} */ + @Override public boolean isEmpty() { + return F.isEmpty(preds) ? col.isEmpty() : !iterator().hasNext(); + } + + /** {@inheritDoc} */ + @Override public String toString() { + return S.toString(PredicateCollectionView.class, this); + } +} http://git-wip-us.apache.org/repos/asf/ignite/blob/a3f845b0/modules/core/src/main/java/org/apache/ignite/internal/util/lang/gridfunc/PredicateMapView.java ---------------------------------------------------------------------- diff --git a/modules/core/src/main/java/org/apache/ignite/internal/util/lang/gridfunc/PredicateMapView.java b/modules/core/src/main/java/org/apache/ignite/internal/util/lang/gridfunc/PredicateMapView.java new file mode 100644 index 0000000..d5b97a6 --- /dev/null +++ b/modules/core/src/main/java/org/apache/ignite/internal/util/lang/gridfunc/PredicateMapView.java @@ -0,0 +1,121 @@ +/* + * 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.ignite.internal.util.lang.gridfunc; + +import java.util.Iterator; +import java.util.Map; +import java.util.Set; +import org.apache.ignite.internal.util.GridSerializableMap; +import org.apache.ignite.internal.util.GridSerializableSet; +import org.apache.ignite.internal.util.lang.GridFunc; +import org.apache.ignite.internal.util.typedef.F; +import org.apache.ignite.internal.util.typedef.internal.S; +import org.apache.ignite.lang.IgnitePredicate; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; + +/** + * Light-weight view on given map with provided predicate. + * + * @param <K> Type of the key. + * @param <V> Type of the value. + */ +public class PredicateMapView<K, V> extends GridSerializableMap<K, V> { + /** */ + private static final long serialVersionUID = 5531745605372387948L; + + /** */ + private final Map<K, V> map; + + /** */ + private final IgnitePredicate<? super K>[] preds; + + /** Entry predicate. */ + private IgnitePredicate<Entry<K, V>> entryPred; + + /** + * @param map Input map that serves as a base for the view. + * @param preds Optional predicates. If predicates are not provided - all will be in the view. + */ + @SuppressWarnings({"unchecked"}) + public PredicateMapView(Map<K, V> map, IgnitePredicate<? super K>... preds) { + this.map = map; + this.preds = preds; + this.entryPred = new EntryByKeyEvaluationPredicate(preds); + } + + /** {@inheritDoc} */ + @NotNull @Override public Set<Entry<K, V>> entrySet() { + return new GridSerializableSet<Entry<K, V>>() { + @NotNull + @Override public Iterator<Entry<K, V>> iterator() { + return GridFunc.iterator0(map.entrySet(), false, entryPred); + } + + @Override public int size() { + return F.size(map.keySet(), preds); + } + + @SuppressWarnings({"unchecked"}) + @Override public boolean remove(Object o) { + return F.isAll((Entry<K, V>)o, entryPred) && map.entrySet().remove(o); + } + + @SuppressWarnings({"unchecked"}) + @Override public boolean contains(Object o) { + return F.isAll((Entry<K, V>)o, entryPred) && map.entrySet().contains(o); + } + + @Override public boolean isEmpty() { + return !iterator().hasNext(); + } + }; + } + + /** {@inheritDoc} */ + @Override public boolean isEmpty() { + return entrySet().isEmpty(); + } + + /** {@inheritDoc} */ + @SuppressWarnings({"unchecked"}) + @Nullable @Override public V get(Object key) { + return GridFunc.isAll((K)key, preds) ? map.get(key) : null; + } + + /** {@inheritDoc} */ + @Nullable @Override public V put(K key, V val) { + V oldVal = get(key); + + if (GridFunc.isAll(key, preds)) + map.put(key, val); + + return oldVal; + } + + /** {@inheritDoc} */ + @SuppressWarnings({"unchecked"}) + @Override public boolean containsKey(Object key) { + return GridFunc.isAll((K)key, preds) && map.containsKey(key); + } + + /** {@inheritDoc} */ + @Override public String toString() { + return S.toString(PredicateMapView.class, this); + } +} http://git-wip-us.apache.org/repos/asf/ignite/blob/a3f845b0/modules/core/src/main/java/org/apache/ignite/internal/util/lang/gridfunc/PredicateSetView.java ---------------------------------------------------------------------- diff --git a/modules/core/src/main/java/org/apache/ignite/internal/util/lang/gridfunc/PredicateSetView.java b/modules/core/src/main/java/org/apache/ignite/internal/util/lang/gridfunc/PredicateSetView.java new file mode 100644 index 0000000..99fc2fd --- /dev/null +++ b/modules/core/src/main/java/org/apache/ignite/internal/util/lang/gridfunc/PredicateSetView.java @@ -0,0 +1,153 @@ +/* + * 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.ignite.internal.util.lang.gridfunc; + +import java.util.Iterator; +import java.util.Set; +import org.apache.ignite.internal.util.GridSerializableMap; +import org.apache.ignite.internal.util.GridSerializableSet; +import org.apache.ignite.internal.util.lang.GridFunc; +import org.apache.ignite.internal.util.typedef.F; +import org.apache.ignite.internal.util.typedef.internal.S; +import org.apache.ignite.lang.IgniteClosure; +import org.apache.ignite.lang.IgnitePredicate; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; + +/** + * Light-weight view on given map with provided preds and mapping. + * + * @param <K> Key type. + * @param <V> Value type. + */ +public class PredicateSetView<K, V> extends GridSerializableMap<K, V> { + /** */ + private static final long serialVersionUID = 0L; + + /** */ + private final Set<K> set; + + /** */ + private final IgniteClosure<? super K, V> clo; + + /** */ + private final IgnitePredicate<? super K>[] preds; + + /** Entry predicate. */ + private IgnitePredicate<K> entryPred; + + /** + * @param set Input collection. + * @param clo Mapping closure, that maps key to value. + * @param preds Optional predicates to filter input collection. If predicates are not provided - all elements + * will be in + */ + @SuppressWarnings({"unchecked"}) + public PredicateSetView(Set<K> set, IgniteClosure<? super K, V> clo, + IgnitePredicate<? super K>... preds) { + this.set = set; + this.clo = clo; + this.preds = preds; + this.entryPred = new IsAllPredicate(preds); + } + + /** {@inheritDoc} */ + @NotNull @Override public Set<Entry<K, V>> entrySet() { + return new GridSerializableSet<Entry<K, V>>() { + @NotNull @Override public Iterator<Entry<K, V>> iterator() { + return new Iterator<Entry<K, V>>() { + + private Iterator<K> it = GridFunc.iterator0(set, true, entryPred); + + @Override public boolean hasNext() { + return it.hasNext(); + } + + @Override public Entry<K, V> next() { + final K e = it.next(); + + return new Entry<K, V>() { + @Override public K getKey() { + return e; + } + + @Override public V getValue() { + return clo.apply(e); + } + + @Override public V setValue(V val) { + throw new UnsupportedOperationException( + "Put is not supported for readonly collection view."); + } + }; + } + + @Override public void remove() { + throw new UnsupportedOperationException( + "Remove is not support for readonly collection view."); + } + }; + } + + @Override public int size() { + return F.size(set, preds); + } + + @Override public boolean remove(Object o) { + throw new UnsupportedOperationException("Remove is not support for readonly collection view."); + } + + @Override public boolean isEmpty() { + return !iterator().hasNext(); + } + }; + } + + /** {@inheritDoc} */ + @Override public boolean isEmpty() { + return entrySet().isEmpty(); + } + + /** {@inheritDoc} */ + @Nullable @Override public V get(Object key) { + if (containsKey(key)) + return clo.apply((K)key); + + return null; + } + + /** {@inheritDoc} */ + @Nullable @Override public V put(K key, V val) { + throw new UnsupportedOperationException("Put is not supported for readonly collection view."); + } + + /** {@inheritDoc} */ + @Override public V remove(Object key) { + throw new UnsupportedOperationException("Remove is not supported for readonly collection view."); + } + + /** {@inheritDoc} */ + @Override public boolean containsKey(Object key) { + return GridFunc.isAll((K)key, preds) && set.contains(key); + } + + /** {@inheritDoc} */ + @Override public String toString() { + return S.toString(PredicateSetView.class, this); + } +} http://git-wip-us.apache.org/repos/asf/ignite/blob/a3f845b0/modules/core/src/main/java/org/apache/ignite/internal/util/lang/gridfunc/ReadOnlyCollectionView.java ---------------------------------------------------------------------- diff --git a/modules/core/src/main/java/org/apache/ignite/internal/util/lang/gridfunc/ReadOnlyCollectionView.java b/modules/core/src/main/java/org/apache/ignite/internal/util/lang/gridfunc/ReadOnlyCollectionView.java new file mode 100644 index 0000000..8186914 --- /dev/null +++ b/modules/core/src/main/java/org/apache/ignite/internal/util/lang/gridfunc/ReadOnlyCollectionView.java @@ -0,0 +1,95 @@ +/* + * 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.ignite.internal.util.lang.gridfunc; + +import java.util.Collection; +import java.util.Iterator; +import org.apache.ignite.internal.util.GridSerializableCollection; +import org.apache.ignite.internal.util.GridSerializableIterator; +import org.apache.ignite.internal.util.lang.GridFunc; +import org.apache.ignite.internal.util.typedef.internal.S; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; + +/** + * Collection wrapper. + * A read-only view will be created over the element and given + * col and no copying will happen. + * + * @param <T> Element type. + */ +public class ReadOnlyCollectionView<T> extends GridSerializableCollection<T> { + /** */ + private static final long serialVersionUID = 0L; + + /** Collection. */ + private final Collection<T> col; + + /** First element in the col. */ + private final T elem; + + /** + * @param col Collection to wrap. + * @param elem First element. + */ + public ReadOnlyCollectionView(@NotNull Collection<T> col, @NotNull T elem) { + this.col = col; + this.elem = elem; + } + + /** {@inheritDoc} */ + @NotNull + @Override public Iterator<T> iterator() { + return new GridSerializableIterator<T>() { + private Iterator<T> it; + + @Override public boolean hasNext() { + return it == null || it.hasNext(); + } + + @Nullable @Override public T next() { + if (it == null) { + it = col.iterator(); + + return elem; + } + + return it.next(); + } + + @Override public void remove() { + throw new UnsupportedOperationException(); + } + }; + } + + /** {@inheritDoc} */ + @Override public int size() { + return col.size() + 1; + } + + /** {@inheritDoc} */ + @Override public boolean equals(Object obj) { + return obj instanceof Collection && GridFunc.eqNotOrdered(this, (Collection)obj); + } + + /** {@inheritDoc} */ + @Override public String toString() { + return S.toString(ReadOnlyCollectionView.class, this); + } +} http://git-wip-us.apache.org/repos/asf/ignite/blob/a3f845b0/modules/core/src/main/java/org/apache/ignite/internal/util/lang/gridfunc/ReadOnlyCollectionView2X.java ---------------------------------------------------------------------- diff --git a/modules/core/src/main/java/org/apache/ignite/internal/util/lang/gridfunc/ReadOnlyCollectionView2X.java b/modules/core/src/main/java/org/apache/ignite/internal/util/lang/gridfunc/ReadOnlyCollectionView2X.java new file mode 100644 index 0000000..82ec651 --- /dev/null +++ b/modules/core/src/main/java/org/apache/ignite/internal/util/lang/gridfunc/ReadOnlyCollectionView2X.java @@ -0,0 +1,100 @@ +/* + * 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.ignite.internal.util.lang.gridfunc; + +import java.util.Collection; +import java.util.Iterator; +import org.apache.ignite.internal.util.GridSerializableCollection; +import org.apache.ignite.internal.util.GridSerializableIterator; +import org.apache.ignite.internal.util.lang.GridFunc; +import org.apache.ignite.internal.util.typedef.internal.S; +import org.jetbrains.annotations.NotNull; + +/** + * Collections wrapper. + * A read-only view will be created over the element and given + * collections and no copying will happen. + * + * @param <T> Element type. + */ +public class ReadOnlyCollectionView2X<T> extends GridSerializableCollection<T> { + /** */ + private static final long serialVersionUID = 0L; + + /** First collection. */ + private final Collection<T> c1; + + /** SecondCollection. */ + private final Collection<T> c2; + + /** + * @param c1 First collection. + * @param c2 SecondCollection. + */ + public ReadOnlyCollectionView2X(Collection<T> c1, Collection<T> c2) { + this.c1 = c1; + this.c2 = c2; + } + + /** {@inheritDoc} */ + @NotNull + @Override public Iterator<T> iterator() { + return new GridSerializableIterator<T>() { + private Iterator<T> it1 = c1.iterator(); + private Iterator<T> it2 = c2.iterator(); + + @Override public boolean hasNext() { + if (it1 != null) + if (!it1.hasNext()) + it1 = null; + else + return true; + + return it2.hasNext(); + } + + @Override public T next() { + return it1 != null ? it1.next() : it2.next(); + } + + @Override public void remove() { + throw new UnsupportedOperationException(); + } + }; + } + + /** {@inheritDoc} */ + @Override public boolean contains(Object o) { + return c1.contains(o) || c2.contains(o); + } + + /** {@inheritDoc} */ + @Override public int size() { + return c1.size() + c2.size(); + } + + /** {@inheritDoc} */ + @Override public boolean equals(Object obj) { + return obj instanceof Collection && GridFunc.eqNotOrdered(this, (Collection<?>)obj); + } + + /** {@inheritDoc} */ + @Override public String toString() { + return S.toString(ReadOnlyCollectionView2X.class, this); + } +} http://git-wip-us.apache.org/repos/asf/ignite/blob/a3f845b0/modules/core/src/main/java/org/apache/ignite/internal/util/lang/gridfunc/SetFactoryCallable.java ---------------------------------------------------------------------- diff --git a/modules/core/src/main/java/org/apache/ignite/internal/util/lang/gridfunc/SetFactoryCallable.java b/modules/core/src/main/java/org/apache/ignite/internal/util/lang/gridfunc/SetFactoryCallable.java new file mode 100644 index 0000000..17ac68c --- /dev/null +++ b/modules/core/src/main/java/org/apache/ignite/internal/util/lang/gridfunc/SetFactoryCallable.java @@ -0,0 +1,41 @@ +/* + * 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.ignite.internal.util.lang.gridfunc; + +import java.util.HashSet; +import java.util.Set; +import org.apache.ignite.internal.util.typedef.internal.S; +import org.apache.ignite.lang.IgniteCallable; + +/** + * Hash set factory. + */ +public class SetFactoryCallable implements IgniteCallable<Set> { + /** */ + private static final long serialVersionUID = 0L; + + /** {@inheritDoc} */ + @Override public Set call() { + return new HashSet(); + } + + /** {@inheritDoc} */ + @Override public String toString() { + return S.toString(SetFactoryCallable.class, this); + } +} http://git-wip-us.apache.org/repos/asf/ignite/blob/a3f845b0/modules/core/src/main/java/org/apache/ignite/internal/util/lang/gridfunc/StringConcatReducer.java ---------------------------------------------------------------------- diff --git a/modules/core/src/main/java/org/apache/ignite/internal/util/lang/gridfunc/StringConcatReducer.java b/modules/core/src/main/java/org/apache/ignite/internal/util/lang/gridfunc/StringConcatReducer.java new file mode 100644 index 0000000..4a2b2a3 --- /dev/null +++ b/modules/core/src/main/java/org/apache/ignite/internal/util/lang/gridfunc/StringConcatReducer.java @@ -0,0 +1,79 @@ +/* + * 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.ignite.internal.util.lang.gridfunc; + +import org.apache.ignite.internal.util.lang.GridFunc; +import org.apache.ignite.internal.util.typedef.internal.S; +import org.apache.ignite.internal.util.typedef.internal.SB; +import org.apache.ignite.lang.IgniteReducer; + +/** + * Reducer that concatenates strings using provided delimiter. + */ +public class StringConcatReducer implements IgniteReducer<String, String> { + /** */ + private static final long serialVersionUID = 0L; + + /** */ + private final String delim; + + /** */ + private SB sb; + + /** */ + private boolean first; + + /** */ + private final Object lock; + + /** + * @param delim Delimiter (optional). + */ + public StringConcatReducer(String delim) { + this.delim = delim; + sb = new SB(); + first = true; + lock = new Object(); + } + + /** {@inheritDoc} */ + @Override public boolean collect(String s) { + synchronized (lock) { + if (!first && !GridFunc.isEmpty(delim)) + sb.a(delim); + + sb.a(s); + + first = false; + } + + return true; + } + + /** {@inheritDoc} */ + @Override public String reduce() { + synchronized (lock) { + return sb.toString(); + } + } + + /** {@inheritDoc} */ + @Override public String toString() { + return S.toString(StringConcatReducer.class, this); + } +} http://git-wip-us.apache.org/repos/asf/ignite/blob/a3f845b0/modules/core/src/main/java/org/apache/ignite/internal/util/lang/gridfunc/ToStringClosure.java ---------------------------------------------------------------------- diff --git a/modules/core/src/main/java/org/apache/ignite/internal/util/lang/gridfunc/ToStringClosure.java b/modules/core/src/main/java/org/apache/ignite/internal/util/lang/gridfunc/ToStringClosure.java new file mode 100644 index 0000000..7cf47cb --- /dev/null +++ b/modules/core/src/main/java/org/apache/ignite/internal/util/lang/gridfunc/ToStringClosure.java @@ -0,0 +1,42 @@ +/* + * 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.ignite.internal.util.lang.gridfunc; + +import org.apache.ignite.internal.util.typedef.C1; +import org.apache.ignite.internal.util.typedef.internal.S; +import org.jetbrains.annotations.Nullable; + +/** + * Closure that return {@code toString()} value for its free variable. + * + * @param <T> Type of the free variable for the closure. + */ +public class ToStringClosure<T> implements C1<T, String> { + /** */ + private static final long serialVersionUID = 0L; + + /** {@inheritDoc} */ + @Override public String apply(@Nullable T t) { + return String.valueOf(t); // This is null-safe. + } + + /** {@inheritDoc} */ + @Override public String toString() { + return S.toString(ToStringClosure.class, this); + } +} http://git-wip-us.apache.org/repos/asf/ignite/blob/a3f845b0/modules/core/src/main/java/org/apache/ignite/internal/util/lang/gridfunc/TransformCollectionView.java ---------------------------------------------------------------------- diff --git a/modules/core/src/main/java/org/apache/ignite/internal/util/lang/gridfunc/TransformCollectionView.java b/modules/core/src/main/java/org/apache/ignite/internal/util/lang/gridfunc/TransformCollectionView.java new file mode 100644 index 0000000..2c317df --- /dev/null +++ b/modules/core/src/main/java/org/apache/ignite/internal/util/lang/gridfunc/TransformCollectionView.java @@ -0,0 +1,79 @@ +/* + * 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.ignite.internal.util.lang.gridfunc; + +import java.util.Collection; +import java.util.Iterator; +import org.apache.ignite.internal.util.GridSerializableCollection; +import org.apache.ignite.internal.util.typedef.F; +import org.apache.ignite.internal.util.typedef.internal.S; +import org.apache.ignite.lang.IgniteClosure; +import org.apache.ignite.lang.IgnitePredicate; +import org.jetbrains.annotations.NotNull; + +/** + * Light-weight view on given collection with provided predicate. + * + * @param <T1> Element type after transformation. + * @param <T2> Element type. + */ +public class TransformCollectionView<T1, T2> extends GridSerializableCollection<T1> { + /** */ + private static final long serialVersionUID = 0L; + + /** */ + private final Collection<? extends T2> col; + + /** */ + private final IgniteClosure<? super T2, T1> clos; + + /** */ + private final IgnitePredicate<? super T2>[] preds; + + /** + * @param col Input collection that serves as a base for the view. + * @param clos Transformation closure. + * @param preds Optional predicated. If predicates are not provided - all elements will be in the view. + */ + public TransformCollectionView(Collection<? extends T2> col, + IgniteClosure<? super T2, T1> clos, IgnitePredicate<? super T2>... preds) { + this.col = col; + this.clos = clos; + this.preds = preds; + } + + /** {@inheritDoc} */ + @NotNull @Override public Iterator<T1> iterator() { + return F.<T2, T1>iterator(col, clos, true, preds); + } + + /** {@inheritDoc} */ + @Override public int size() { + return F.isEmpty(preds) ? col.size() : F.size(iterator()); + } + + /** {@inheritDoc} */ + @Override public boolean isEmpty() { + return F.isEmpty(preds) ? col.isEmpty() : !iterator().hasNext(); + } + + /** {@inheritDoc} */ + @Override public String toString() { + return S.toString(TransformCollectionView.class, this); + } +} http://git-wip-us.apache.org/repos/asf/ignite/blob/a3f845b0/modules/core/src/main/java/org/apache/ignite/internal/util/lang/gridfunc/TransformFilteringIterator.java ---------------------------------------------------------------------- diff --git a/modules/core/src/main/java/org/apache/ignite/internal/util/lang/gridfunc/TransformFilteringIterator.java b/modules/core/src/main/java/org/apache/ignite/internal/util/lang/gridfunc/TransformFilteringIterator.java new file mode 100644 index 0000000..9ddd729 --- /dev/null +++ b/modules/core/src/main/java/org/apache/ignite/internal/util/lang/gridfunc/TransformFilteringIterator.java @@ -0,0 +1,138 @@ +/* + * 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.ignite.internal.util.lang.gridfunc; + +import java.util.Iterator; +import java.util.NoSuchElementException; +import org.apache.ignite.internal.util.lang.GridFunc; +import org.apache.ignite.internal.util.lang.GridIteratorAdapter; +import org.apache.ignite.internal.util.typedef.internal.S; +import org.apache.ignite.lang.IgniteClosure; +import org.apache.ignite.lang.IgnitePredicate; +import org.jetbrains.annotations.Nullable; + +/** + * Iterator from given iter and optional filtering predicate. + */ +public class TransformFilteringIterator<T2, T1> extends GridIteratorAdapter<T2> { + /** */ + private static final long serialVersionUID = 0L; + + /** */ + private final IgniteClosure<? super T1, T2> clos; + + /** */ + private final boolean readOnly; + + /** */ + private final IgnitePredicate<? super T1>[] preds; + + /** */ + private T1 elem; + + /** */ + private boolean more; + + /** */ + private boolean moved; + + /** */ + private Iterator<? extends T1> iter; + + /** + * @param iter Input iter. + * @param clos Transforming closure to convert from T1 to T2. + * @param readOnly If {@code true}, then resulting iter will not allow modifications to the underlying + * collection. + * @param preds Optional filtering predicates. + */ + public TransformFilteringIterator(Iterator<? extends T1> iter, IgniteClosure<? super T1, T2> clos, + boolean readOnly, + IgnitePredicate<? super T1>... preds) { + this.clos = clos; + this.readOnly = readOnly; + this.preds = preds; + this.iter = iter; + this.moved = true; + } + + /** {@inheritDoc} */ + @Override public boolean hasNextX() { + if (GridFunc.isEmpty(preds)) + return iter.hasNext(); + else { + if (!moved) + return more; + else { + more = false; + + while (iter.hasNext()) { + elem = iter.next(); + + boolean isAll = true; + + for (IgnitePredicate<? super T1> r : preds) + if (r != null && !r.apply(elem)) { + isAll = false; + + break; + } + + if (isAll) { + more = true; + moved = false; + + return true; + } + } + + elem = null; // Give to GC. + + return false; + } + } + } + + /** {@inheritDoc} */ + @Nullable @Override public T2 nextX() { + if (GridFunc.isEmpty(preds)) + return clos.apply(iter.next()); + else { + if (hasNext()) { + moved = true; + + return clos.apply(elem); + } + else + throw new NoSuchElementException(); + } + } + + /** {@inheritDoc} */ + @Override public void removeX() { + if (readOnly) + throw new UnsupportedOperationException("Cannot modify read-only iter."); + + iter.remove(); + } + + /** {@inheritDoc} */ + @Override public String toString() { + return S.toString(TransformFilteringIterator.class, this); + } +} http://git-wip-us.apache.org/repos/asf/ignite/blob/a3f845b0/modules/core/src/main/java/org/apache/ignite/internal/util/lang/gridfunc/TransformMapView.java ---------------------------------------------------------------------- diff --git a/modules/core/src/main/java/org/apache/ignite/internal/util/lang/gridfunc/TransformMapView.java b/modules/core/src/main/java/org/apache/ignite/internal/util/lang/gridfunc/TransformMapView.java new file mode 100644 index 0000000..91b8302 --- /dev/null +++ b/modules/core/src/main/java/org/apache/ignite/internal/util/lang/gridfunc/TransformMapView.java @@ -0,0 +1,168 @@ +/* + * 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.ignite.internal.util.lang.gridfunc; + +import java.util.Iterator; +import java.util.Map; +import java.util.Set; +import org.apache.ignite.internal.util.GridSerializableMap; +import org.apache.ignite.internal.util.GridSerializableSet; +import org.apache.ignite.internal.util.lang.GridFunc; +import org.apache.ignite.internal.util.typedef.F; +import org.apache.ignite.internal.util.typedef.internal.S; +import org.apache.ignite.lang.IgniteClosure; +import org.apache.ignite.lang.IgnitePredicate; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; + +/** + * Light-weight view on given map with provided predicate and clos. + * + * @param <K> Type of the key. + * @param <V> Type of the input map value. + * @param <V1> Type of the output map value. + */ +public class TransformMapView<K, V1, V> extends GridSerializableMap<K, V1> { + /** */ + private static final long serialVersionUID = 0L; + + /** */ + private final Map<K, V> map; + + /** */ + private final IgniteClosure<V, V1> clos; + + /** */ + private final boolean hasPred; + + /** */ + private final IgnitePredicate<? super K>[] preds; + + /** Entry predicate. */ + private IgnitePredicate<Entry<K, V>> entryPred; + + /** + * @param map Input map that serves as a base for the view. + * @param clos Transformer for map value transformation. + * @param preds Optional predicates. If predicates are not provided - all will be in the view. + */ + @SuppressWarnings({"unchecked"}) + public TransformMapView(Map<K, V> map, IgniteClosure<V, V1> clos, + IgnitePredicate<? super K>... preds) { + this.map = map; + this.clos = clos; + this.hasPred = (preds != null && preds.length > 0); + this.preds = preds; + this.entryPred = new EntryByKeyEvaluationPredicate(preds); + } + + /** {@inheritDoc} */ + @NotNull @Override public Set<Entry<K, V1>> entrySet() { + return new GridSerializableSet<Entry<K, V1>>() { + @NotNull + @Override public Iterator<Entry<K, V1>> iterator() { + return new Iterator<Entry<K, V1>>() { + private Iterator<Entry<K, V>> iter = GridFunc.iterator0(map.entrySet(), true, entryPred); + + @Override public boolean hasNext() { + return iter.hasNext(); + } + + @Override public Entry<K, V1> next() { + final Entry<K, V> e = iter.next(); + + return new Entry<K, V1>() { + @Override public K getKey() { + return e.getKey(); + } + + @Override public V1 getValue() { + return clos.apply(e.getValue()); + } + + @Override public V1 setValue(V1 val) { + throw new UnsupportedOperationException("Put is not supported for readonly map view."); + } + }; + } + + @Override public void remove() { + throw new UnsupportedOperationException("Remove is not support for readonly map view."); + } + }; + } + + @Override public int size() { + return hasPred ? F.size(map.keySet(), preds) : map.size(); + } + + @SuppressWarnings({"unchecked"}) + @Override public boolean remove(Object o) { + throw new UnsupportedOperationException("Remove is not support for readonly map view."); + } + + @SuppressWarnings({"unchecked"}) + @Override public boolean contains(Object o) { + return F.isAll((Entry<K, V>)o, entryPred) && map.entrySet().contains(o); + } + + @Override public boolean isEmpty() { + return hasPred ? !iterator().hasNext() : map.isEmpty(); + } + }; + } + + /** {@inheritDoc} */ + @Override public boolean isEmpty() { + return hasPred ? entrySet().isEmpty() : map.isEmpty(); + } + + /** {@inheritDoc} */ + @SuppressWarnings({"unchecked"}) + @Nullable @Override public V1 get(Object key) { + if (GridFunc.isAll((K)key, preds)) { + V v = map.get(key); + + if (v != null) + return clos.apply(v); + } + + return null; + } + + /** {@inheritDoc} */ + @Nullable @Override public V1 put(K key, V1 val) { + throw new UnsupportedOperationException("Put is not supported for readonly map view."); + } + + /** {@inheritDoc} */ + @Override public V1 remove(Object key) { + throw new UnsupportedOperationException("Remove is not supported for readonly map view."); + } + + /** {@inheritDoc} */ + @SuppressWarnings({"unchecked"}) + @Override public boolean containsKey(Object key) { + return GridFunc.isAll((K)key, preds) && map.containsKey(key); + } + + /** {@inheritDoc} */ + @Override public String toString() { + return S.toString(TransformMapView.class, this); + } +} http://git-wip-us.apache.org/repos/asf/ignite/blob/a3f845b0/modules/core/src/main/java/org/apache/ignite/internal/util/lang/gridfunc/TransformMapView2.java ---------------------------------------------------------------------- diff --git a/modules/core/src/main/java/org/apache/ignite/internal/util/lang/gridfunc/TransformMapView2.java b/modules/core/src/main/java/org/apache/ignite/internal/util/lang/gridfunc/TransformMapView2.java new file mode 100644 index 0000000..523d4cc --- /dev/null +++ b/modules/core/src/main/java/org/apache/ignite/internal/util/lang/gridfunc/TransformMapView2.java @@ -0,0 +1,165 @@ +/* + * 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.ignite.internal.util.lang.gridfunc; + +import java.util.Iterator; +import java.util.Map; +import java.util.Set; +import org.apache.ignite.internal.util.GridSerializableMap; +import org.apache.ignite.internal.util.GridSerializableSet; +import org.apache.ignite.internal.util.lang.GridFunc; +import org.apache.ignite.internal.util.typedef.F; +import org.apache.ignite.internal.util.typedef.internal.S; +import org.apache.ignite.lang.IgniteBiClosure; +import org.apache.ignite.lang.IgnitePredicate; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; + +/** + * Light-weight view on given map with provided predicate and clos. + * + * @param <K> Type of the key. + * @param <V> Type of the input map value. + * @param <V1> Type of the output map value. + */ +public class TransformMapView2<K, V, V1> extends GridSerializableMap<K, V1> { + /** */ + private static final long serialVersionUID = 0L; + + /** */ + private final Map<K, V> map; + + /** */ + private final IgniteBiClosure<K, V, V1> clos; + + /** */ + private final IgnitePredicate<? super K>[] preds; + + /** Entry predicate. */ + private IgnitePredicate<Entry<K, V>> entryPred; + + /** + * @param map Input map that serves as a base for the view. + * @param clos Transformer for map value transformation. + * @param preds Optional predicates. If predicates are not provided - all will be in the view. + */ + @SuppressWarnings({"unchecked"}) + public TransformMapView2(Map<K, V> map, IgniteBiClosure<K, V, V1> clos, + IgnitePredicate<? super K>... preds) { + this.map = map; + this.clos = clos; + this.preds = preds; + entryPred = new EntryByKeyEvaluationPredicate(preds); + } + + /** {@inheritDoc} */ + @NotNull @Override public Set<Entry<K, V1>> entrySet() { + return new GridSerializableSet<Entry<K, V1>>() { + @NotNull + @Override public Iterator<Entry<K, V1>> iterator() { + return new Iterator<Entry<K, V1>>() { + private Iterator<Entry<K, V>> it = GridFunc.iterator0(map.entrySet(), true, entryPred); + + @Override public boolean hasNext() { + return it.hasNext(); + } + + @Override public Entry<K, V1> next() { + final Entry<K, V> e = it.next(); + + return new Entry<K, V1>() { + @Override public K getKey() { + return e.getKey(); + } + + @Override public V1 getValue() { + return clos.apply(e.getKey(), e.getValue()); + } + + @Override public V1 setValue(V1 val) { + throw new UnsupportedOperationException( + "Put is not supported for readonly map view."); + } + }; + } + + @Override public void remove() { + throw new UnsupportedOperationException("Remove is not support for readonly map view."); + } + }; + } + + @Override public int size() { + return F.size(map.keySet(), preds); + } + + @SuppressWarnings({"unchecked"}) + @Override public boolean remove(Object o) { + throw new UnsupportedOperationException("Remove is not support for readonly map view."); + } + + @SuppressWarnings({"unchecked"}) + @Override public boolean contains(Object o) { + return F.isAll((Entry<K, V>)o, entryPred) && map.entrySet().contains(o); + } + + @Override public boolean isEmpty() { + return !iterator().hasNext(); + } + }; + } + + /** {@inheritDoc} */ + @Override public boolean isEmpty() { + return entrySet().isEmpty(); + } + + /** {@inheritDoc} */ + @SuppressWarnings({"unchecked"}) + @Nullable @Override public V1 get(Object key) { + if (GridFunc.isAll((K)key, preds)) { + V v = map.get(key); + + if (v != null) + return clos.apply((K)key, v); + } + + return null; + } + + /** {@inheritDoc} */ + @Nullable @Override public V1 put(K key, V1 val) { + throw new UnsupportedOperationException("Put is not supported for readonly map view."); + } + + /** {@inheritDoc} */ + @Override public V1 remove(Object key) { + throw new UnsupportedOperationException("Remove is not supported for readonly map view."); + } + + /** {@inheritDoc} */ + @SuppressWarnings({"unchecked"}) + @Override public boolean containsKey(Object key) { + return GridFunc.isAll((K)key, preds) && map.containsKey(key); + } + + /** {@inheritDoc} */ + @Override public String toString() { + return S.toString(TransformMapView2.class, this); + } +} http://git-wip-us.apache.org/repos/asf/ignite/blob/a3f845b0/modules/core/src/main/java/org/apache/ignite/internal/util/lang/gridfunc/UuidToId8Closure.java ---------------------------------------------------------------------- diff --git a/modules/core/src/main/java/org/apache/ignite/internal/util/lang/gridfunc/UuidToId8Closure.java b/modules/core/src/main/java/org/apache/ignite/internal/util/lang/gridfunc/UuidToId8Closure.java new file mode 100644 index 0000000..dcf165b --- /dev/null +++ b/modules/core/src/main/java/org/apache/ignite/internal/util/lang/gridfunc/UuidToId8Closure.java @@ -0,0 +1,41 @@ +/* + * 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.ignite.internal.util.lang.gridfunc; + +import java.util.UUID; +import org.apache.ignite.internal.util.typedef.internal.S; +import org.apache.ignite.internal.util.typedef.internal.U; +import org.apache.ignite.lang.IgniteClosure; + +/** + * UUID to ID8 transformer closure. + */ +public class UuidToId8Closure implements IgniteClosure<UUID, String> { + /** */ + private static final long serialVersionUID = 0L; + + /** {@inheritDoc} */ + @Override public String apply(UUID id) { + return U.id8(id); + } + + /** {@inheritDoc} */ + @Override public String toString() { + return S.toString(UuidToId8Closure.class, this); + } +} http://git-wip-us.apache.org/repos/asf/ignite/blob/a3f845b0/modules/indexing/src/test/java/org/apache/ignite/internal/processors/query/h2/GridIndexingSpiAbstractSelfTest.java ---------------------------------------------------------------------- diff --git a/modules/indexing/src/test/java/org/apache/ignite/internal/processors/query/h2/GridIndexingSpiAbstractSelfTest.java b/modules/indexing/src/test/java/org/apache/ignite/internal/processors/query/h2/GridIndexingSpiAbstractSelfTest.java index 6b745ab..68d6bf1 100644 --- a/modules/indexing/src/test/java/org/apache/ignite/internal/processors/query/h2/GridIndexingSpiAbstractSelfTest.java +++ b/modules/indexing/src/test/java/org/apache/ignite/internal/processors/query/h2/GridIndexingSpiAbstractSelfTest.java @@ -454,8 +454,6 @@ public abstract class GridIndexingSpiAbstractSelfTest extends GridCommonAbstract String res = log.toString(); - F.println(res); - assertTrue(res.contains("/* PUBLIC.RANGE_INDEX */")); } finally {
