• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright (c) 2008, SnakeYAML
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
5  * in compliance with the License. You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software distributed under the License
10  * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
11  * or implied. See the License for the specific language governing permissions and limitations under
12  * the License.
13  */
14 package org.yaml.snakeyaml.util;
15 
16 import java.util.AbstractList;
17 import java.util.Collections;
18 import java.util.List;
19 
20 public class ArrayUtils {
21 
ArrayUtils()22   private ArrayUtils() {}
23 
24   /**
25    * Returns an unmodifiable {@code List} backed by the given array. The method doesn't copy the
26    * array, so the changes to the array will affect the {@code List} as well.
27    *
28    * @param <E> class of the elements in the array
29    * @param elements - array to convert
30    * @return {@code List} backed by the given array
31    */
toUnmodifiableList(E[] elements)32   public static <E> List<E> toUnmodifiableList(E[] elements) {
33     return elements.length == 0 ? Collections.<E>emptyList()
34         : new UnmodifiableArrayList<E>(elements);
35   }
36 
37   /**
38    * Returns an unmodifiable {@code List} containing the second array appended to the first one. The
39    * method doesn't copy the arrays, so the changes to the arrays will affect the {@code List} as
40    * well.
41    *
42    * @param <E> class of the elements in the array
43    * @param array1 - the array to extend
44    * @param array2 - the array to add to the first
45    * @return {@code List} backed by the given arrays
46    */
toUnmodifiableCompositeList(E[] array1, E[] array2)47   public static <E> List<E> toUnmodifiableCompositeList(E[] array1, E[] array2) {
48     List<E> result;
49     if (array1.length == 0) {
50       result = toUnmodifiableList(array2);
51     } else if (array2.length == 0) {
52       result = toUnmodifiableList(array1);
53     } else {
54       result = new CompositeUnmodifiableArrayList<E>(array1, array2);
55     }
56     return result;
57   }
58 
59   private static class UnmodifiableArrayList<E> extends AbstractList<E> {
60 
61     private final E[] array;
62 
UnmodifiableArrayList(E[] array)63     UnmodifiableArrayList(E[] array) {
64       this.array = array;
65     }
66 
67     @Override
get(int index)68     public E get(int index) {
69       if (index >= array.length) {
70         throw new IndexOutOfBoundsException("Index: " + index + ", Size: " + size());
71       }
72       return array[index];
73     }
74 
75     @Override
size()76     public int size() {
77       return array.length;
78     }
79   }
80 
81   private static class CompositeUnmodifiableArrayList<E> extends AbstractList<E> {
82 
83     private final E[] array1;
84     private final E[] array2;
85 
CompositeUnmodifiableArrayList(E[] array1, E[] array2)86     CompositeUnmodifiableArrayList(E[] array1, E[] array2) {
87       this.array1 = array1;
88       this.array2 = array2;
89     }
90 
91     @Override
get(int index)92     public E get(int index) {
93       E element;
94       if (index < array1.length) {
95         element = array1[index];
96       } else if (index - array1.length < array2.length) {
97         element = array2[index - array1.length];
98       } else {
99         throw new IndexOutOfBoundsException("Index: " + index + ", Size: " + size());
100       }
101       return element;
102     }
103 
104     @Override
size()105     public int size() {
106       return array1.length + array2.length;
107     }
108   }
109 }
110