This is an automated email from the ASF dual-hosted git repository.

ahuber pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/isis.git


The following commit(s) were added to refs/heads/master by this push:
     new d85f81d  ISIS-2383: add CanVector<T> a mutable, but fixed size vector 
of Can<T>
d85f81d is described below

commit d85f81db3a599974817957e7468a7b32446da362
Author: Andi Huber <ahu...@apache.org>
AuthorDate: Tue Jun 30 09:06:17 2020 +0200

    ISIS-2383: add CanVector<T> a mutable, but fixed size vector of Can<T>
---
 .../isis/core/commons/collections/CanVector.java   | 88 ++++++++++++++++++++++
 .../core/commons/internal/primitives/_Ints.java    |  4 +
 2 files changed, 92 insertions(+)

diff --git 
a/core/commons/src/main/java/org/apache/isis/core/commons/collections/CanVector.java
 
b/core/commons/src/main/java/org/apache/isis/core/commons/collections/CanVector.java
new file mode 100644
index 0000000..a14bf3a
--- /dev/null
+++ 
b/core/commons/src/main/java/org/apache/isis/core/commons/collections/CanVector.java
@@ -0,0 +1,88 @@
+/*
+ *  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.isis.core.commons.collections;
+
+import java.io.Serializable;
+import java.util.ArrayList;
+import java.util.Iterator;
+import java.util.List;
+import java.util.stream.IntStream;
+
+import lombok.NonNull;
+
+/** 
+ * Represents a mutable, but fixed size vector of Can<T>.
+ * <p>
+ * Mutable meaning, that the vector elements can be replaced.
+ * 
+ * @since Jun 30, 2020
+ */
+public final class CanVector<T> implements Iterable<Can<T>>, Serializable {
+
+    private static final long serialVersionUID = 1L;
+    
+    private final int size;
+    private final List<Can<T>> cans;
+    
+    public CanVector(int size) {
+        this.size = size;
+        this.cans = new ArrayList<>(size);
+        // init vector with empty cans
+        IntStream.range(0, size).forEach(__->cans.add(Can.empty()));
+    }
+
+    public int size() {
+        return size;
+    }
+    
+    @Override
+    public Iterator<Can<T>> iterator() {
+        return cans.iterator();
+    }
+
+    /**
+     * Returns the element at the specified position in this CanVector.
+     * @param index
+     * @throws IndexOutOfBoundsException if the index is out of range
+     *         (<tt>index &lt; 0 || index &gt;= size()</tt>)
+     */
+    public Can<T> get(int index) {
+        return cans.get(index);
+    }
+    
+    /**
+     * Replaces the element at the specified position in this CanVector with 
the
+     * specified element.
+     * 
+     * @param index
+     * @param can
+     * @return self
+     * @throws IndexOutOfBoundsException if the index is out of range
+     *         (<tt>index &lt; 0 || index &gt;= size()</tt>)
+     */
+    public CanVector<T> set(int index, @NonNull Can<T> can) {
+        cans.set(index, can);
+        return this;
+    }
+
+    public static <T> CanVector<T> empty() {
+        return new CanVector<>(0);
+    }
+    
+}
diff --git 
a/core/commons/src/main/java/org/apache/isis/core/commons/internal/primitives/_Ints.java
 
b/core/commons/src/main/java/org/apache/isis/core/commons/internal/primitives/_Ints.java
index 5cf569c..7fc86e7 100644
--- 
a/core/commons/src/main/java/org/apache/isis/core/commons/internal/primitives/_Ints.java
+++ 
b/core/commons/src/main/java/org/apache/isis/core/commons/internal/primitives/_Ints.java
@@ -21,6 +21,7 @@ package org.apache.isis.core.commons.internal.primitives;
 import java.util.Optional;
 import java.util.OptionalInt;
 import java.util.function.Consumer;
+import java.util.stream.IntStream;
 
 import javax.annotation.Nullable;
 
@@ -117,6 +118,9 @@ public class _Ints {
                     lowerBound.isInclusive() ? '[' : '(', 
lowerBound.getValue(),
                     upperBound.getValue(), upperBound.isInclusive() ? ']' : 
')');
         }
+        public IntStream stream() {
+            return IntStream.rangeClosed(nearestToLower(), nearestToUpper());
+        }
     }
     
     // -- RANGE FACTORIES

Reply via email to