OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file |
| 2 // for details. All rights reserved. Use of this source code is governed by a |
| 3 // BSD-style license that can be found in the LICENSE file. |
| 4 |
| 5 /* |
| 6 * This is a wrapper around a fixed-length list with modifications disabled. |
| 7 */ |
| 8 class PbImmutableList<T> implements List<T>, Hashable { |
| 9 List<T> _wrappedList; |
| 10 int _memoizedHashCode = -1; |
| 11 |
| 12 static List _EMPTY; |
| 13 static PbImmutableList get EMPTY() { |
| 14 if (_EMPTY == null) { |
| 15 _EMPTY = new PbImmutableList.from([]); |
| 16 } |
| 17 return _EMPTY; |
| 18 } |
| 19 |
| 20 PbImmutableList.from(List<T> list) { |
| 21 _wrappedList = new List(list.length); |
| 22 _wrappedList.setRange(0, list.length, list); |
| 23 } |
| 24 |
| 25 bool operator ==(Object o) { |
| 26 if (o is! PbImmutableList){ |
| 27 return false; |
| 28 } |
| 29 PbImmutableList oil = o; |
| 30 if (length != oil.length) { |
| 31 return false; |
| 32 } |
| 33 |
| 34 for (int i = 0; i < length; i++) { |
| 35 if (this[i] != oil[i]) { |
| 36 return false; |
| 37 } |
| 38 } |
| 39 return true; |
| 40 } |
| 41 |
| 42 int hashCode() { |
| 43 if (_memoizedHashCode == -1) { |
| 44 _memoizedHashCode = 13 * length; |
| 45 for (var entry in this) { |
| 46 if (entry is Hashable) { |
| 47 _memoizedHashCode = (19 * _memoizedHashCode) + entry.hashCode; |
| 48 } |
| 49 } |
| 50 } |
| 51 return _memoizedHashCode; |
| 52 } |
| 53 |
| 54 T operator [](int index) => _wrappedList[index]; |
| 55 |
| 56 List<T> getRange(int start, int length) => |
| 57 _wrappedList.getRange(start, length); |
| 58 |
| 59 int indexOf(T element, [int start = 0]) => |
| 60 _wrappedList.indexOf(element, start); |
| 61 |
| 62 Iterator<T> iterator() => _wrappedList.iterator(); |
| 63 |
| 64 T last() => this[length - 1]; |
| 65 |
| 66 int lastIndexOf(T element, [int start = null]) => |
| 67 _wrappedList.lastIndexOf(element, start); |
| 68 |
| 69 int get length() => _wrappedList.length; |
| 70 |
| 71 String toString() { |
| 72 return "PbImmutableList"; |
| 73 } |
| 74 |
| 75 void operator []=(int index, T value) { |
| 76 throw const UnsupportedOperationException( |
| 77 "Cannot modify an immutable list"); |
| 78 } |
| 79 |
| 80 void copyFrom(List src, int srcStart, int dstStart, int count) { |
| 81 throw const UnsupportedOperationException( |
| 82 "Cannot modify an immutable list"); |
| 83 } |
| 84 |
| 85 void setRange(int start, int length, List<T> from, [int startFrom = 0]) { |
| 86 throw const UnsupportedOperationException( |
| 87 "Cannot modify an immutable list"); |
| 88 } |
| 89 |
| 90 void removeRange(int start, int length) { |
| 91 throw const UnsupportedOperationException( |
| 92 "Cannot remove range of an immutable list"); |
| 93 } |
| 94 |
| 95 void insertRange(int start, int length, [T initialValue = null]) { |
| 96 throw const UnsupportedOperationException( |
| 97 "Cannot insert range in an immutable list"); |
| 98 } |
| 99 |
| 100 void sort(int compare(T a, T b)) { |
| 101 throw const UnsupportedOperationException( |
| 102 "Cannot modify an immutable list"); |
| 103 } |
| 104 |
| 105 void add(T element) { |
| 106 throw const UnsupportedOperationException( |
| 107 "Cannot add to an immutable list"); |
| 108 } |
| 109 |
| 110 void addLast(T element) { |
| 111 add(element); |
| 112 } |
| 113 |
| 114 void addAll(Collection<T> elements) { |
| 115 throw const UnsupportedOperationException( |
| 116 "Cannot add to an immutable list"); |
| 117 } |
| 118 |
| 119 void clear() { |
| 120 throw const UnsupportedOperationException( |
| 121 "Cannot clear an immutable list"); |
| 122 } |
| 123 |
| 124 void set length(int length) { |
| 125 throw const UnsupportedOperationException( |
| 126 "Cannot change the length of an immutable list"); |
| 127 } |
| 128 |
| 129 T removeLast() { |
| 130 throw const UnsupportedOperationException( |
| 131 "Cannot remove in a non-extendable list"); |
| 132 } |
| 133 |
| 134 /** |
| 135 * Collection interface. |
| 136 */ |
| 137 |
| 138 void forEach(f(T element)) => _wrappedList.forEach(f); |
| 139 |
| 140 Collection<T> filter(bool f(T element)) => _wrappedList.filter(f); |
| 141 |
| 142 bool every(bool f(T element)) => _wrappedList.every(f); |
| 143 |
| 144 bool some(bool f(T element)) => _wrappedList.some(f); |
| 145 |
| 146 bool isEmpty() => _wrappedList.isEmpty(); |
| 147 |
| 148 Collection map(f(T element)) { |
| 149 throw "not implemented"; |
| 150 } |
| 151 } |
OLD | NEW |