1090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson/*
21d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * Copyright (C) 2009 The Guava Authors
3090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson *
4090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * Licensed under the Apache License, Version 2.0 (the "License");
5090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * you may not use this file except in compliance with the License.
6090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * You may obtain a copy of the License at
7090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson *
8090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * http://www.apache.org/licenses/LICENSE-2.0
9090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson *
10090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * Unless required by applicable law or agreed to in writing, software
11090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * distributed under the License is distributed on an "AS IS" BASIS,
12090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * See the License for the specific language governing permissions and
14090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * limitations under the License.
15090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson */
16090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
17090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilsonpackage com.google.common.collect;
18090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
19090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilsonimport com.google.common.annotations.GwtCompatible;
20090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilsonimport com.google.common.base.Preconditions;
21090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
22090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilsonimport javax.annotation.Nullable;
23090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
24090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson/**
25090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * Implementation of {@link ImmutableList} with one or more elements.
26090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson *
27090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * @author Kevin Bourrillion
28090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson */
291d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert@GwtCompatible(serializable = true, emulated = true)
300888a09821a98ac0680fad765217302858e70fa4Paul Duffin@SuppressWarnings("serial") // uses writeReplace(), not default serialization
31bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnorclass RegularImmutableList<E> extends ImmutableList<E> {
32090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  private final transient int offset;
33090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  private final transient int size;
34090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  private final transient Object[] array;
35090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
36090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  RegularImmutableList(Object[] array, int offset, int size) {
37090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    this.offset = offset;
38090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    this.size = size;
39090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    this.array = array;
40090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  }
41090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
42090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  RegularImmutableList(Object[] array) {
43090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    this(array, 0, array.length);
44090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  }
45090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
460888a09821a98ac0680fad765217302858e70fa4Paul Duffin  @Override
47090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  public int size() {
48090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    return size;
49090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  }
50090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
510888a09821a98ac0680fad765217302858e70fa4Paul Duffin  @Override boolean isPartialView() {
520888a09821a98ac0680fad765217302858e70fa4Paul Duffin    return size != array.length;
53090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  }
54090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
557dd252788645e940eada959bdde927426e2531c9Paul Duffin  @Override
560888a09821a98ac0680fad765217302858e70fa4Paul Duffin  int copyIntoArray(Object[] dst, int dstOff) {
570888a09821a98ac0680fad765217302858e70fa4Paul Duffin    System.arraycopy(array, offset, dst, dstOff, size);
580888a09821a98ac0680fad765217302858e70fa4Paul Duffin    return dstOff + size;
591d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  }
601d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert
610888a09821a98ac0680fad765217302858e70fa4Paul Duffin  // The fake cast to E is safe because the creation methods only allow E's
627dd252788645e940eada959bdde927426e2531c9Paul Duffin  @Override
630888a09821a98ac0680fad765217302858e70fa4Paul Duffin  @SuppressWarnings("unchecked")
640888a09821a98ac0680fad765217302858e70fa4Paul Duffin  public E get(int index) {
650888a09821a98ac0680fad765217302858e70fa4Paul Duffin    Preconditions.checkElementIndex(index, size);
660888a09821a98ac0680fad765217302858e70fa4Paul Duffin    return (E) array[index + offset];
67090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  }
68090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
697dd252788645e940eada959bdde927426e2531c9Paul Duffin  @Override
700888a09821a98ac0680fad765217302858e70fa4Paul Duffin  public int indexOf(@Nullable Object object) {
710888a09821a98ac0680fad765217302858e70fa4Paul Duffin    if (object == null) {
720888a09821a98ac0680fad765217302858e70fa4Paul Duffin      return -1;
730888a09821a98ac0680fad765217302858e70fa4Paul Duffin    }
740888a09821a98ac0680fad765217302858e70fa4Paul Duffin    for (int i = 0; i < size; i++) {
750888a09821a98ac0680fad765217302858e70fa4Paul Duffin      if (array[offset + i].equals(object)) {
760888a09821a98ac0680fad765217302858e70fa4Paul Duffin        return i;
770888a09821a98ac0680fad765217302858e70fa4Paul Duffin      }
78090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    }
790888a09821a98ac0680fad765217302858e70fa4Paul Duffin    return -1;
80090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  }
81090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
820888a09821a98ac0680fad765217302858e70fa4Paul Duffin  @Override
830888a09821a98ac0680fad765217302858e70fa4Paul Duffin  public int lastIndexOf(@Nullable Object object) {
840888a09821a98ac0680fad765217302858e70fa4Paul Duffin    if (object == null) {
850888a09821a98ac0680fad765217302858e70fa4Paul Duffin      return -1;
860888a09821a98ac0680fad765217302858e70fa4Paul Duffin    }
870888a09821a98ac0680fad765217302858e70fa4Paul Duffin    for (int i = size - 1; i >= 0; i--) {
880888a09821a98ac0680fad765217302858e70fa4Paul Duffin      if (array[offset + i].equals(object)) {
890888a09821a98ac0680fad765217302858e70fa4Paul Duffin        return i;
900888a09821a98ac0680fad765217302858e70fa4Paul Duffin      }
910888a09821a98ac0680fad765217302858e70fa4Paul Duffin    }
920888a09821a98ac0680fad765217302858e70fa4Paul Duffin    return -1;
93090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  }
94090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
957dd252788645e940eada959bdde927426e2531c9Paul Duffin  @Override
967dd252788645e940eada959bdde927426e2531c9Paul Duffin  ImmutableList<E> subListUnchecked(int fromIndex, int toIndex) {
970888a09821a98ac0680fad765217302858e70fa4Paul Duffin    return new RegularImmutableList<E>(
980888a09821a98ac0680fad765217302858e70fa4Paul Duffin        array, offset + fromIndex, toIndex - fromIndex);
99dbd967a6e5c96cc1a97c5521f88dc1564ba2f81bPaul Duffin  }
100dbd967a6e5c96cc1a97c5521f88dc1564ba2f81bPaul Duffin
1017dd252788645e940eada959bdde927426e2531c9Paul Duffin  @SuppressWarnings("unchecked")
1020888a09821a98ac0680fad765217302858e70fa4Paul Duffin  @Override
1037dd252788645e940eada959bdde927426e2531c9Paul Duffin  public UnmodifiableListIterator<E> listIterator(int index) {
1047dd252788645e940eada959bdde927426e2531c9Paul Duffin    // for performance
1057dd252788645e940eada959bdde927426e2531c9Paul Duffin    // The fake cast to E is safe because the creation methods only allow E's
1060888a09821a98ac0680fad765217302858e70fa4Paul Duffin    return (UnmodifiableListIterator<E>)
1070888a09821a98ac0680fad765217302858e70fa4Paul Duffin        Iterators.forArray(array, offset, size, index);
108090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  }
109090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
1100888a09821a98ac0680fad765217302858e70fa4Paul Duffin  // TODO(user): benchmark optimizations for equals() and see if they're worthwhile
111090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson}
112