• R/O
  • SSH
  • HTTPS

Commit

Tags
No Tags

Frequently used words (click to add to your profile)

javac++androidlinuxc#windowsobjective-ccocoa誰得qtpythonphprubygameguibathyscaphec計画中(planning stage)翻訳omegatframeworktwitterdomtestvb.netdirectxゲームエンジンbtronarduinopreviewer

Commit MetaInfo

Revision77 (tree)
Time2015-08-21 01:21:09
Authort_nakayama1971

Log Message

(empty log message)

Change Summary

Incremental Difference

--- trunk/framework/trial/src/core/util/bean/Tuple.java (nonexistent)
+++ trunk/framework/trial/src/core/util/bean/Tuple.java (revision 77)
@@ -0,0 +1,172 @@
1+package core.util.bean;
2+
3+import java.io.Serializable;
4+import java.util.Arrays;
5+import java.util.Iterator;
6+import java.util.NoSuchElementException;
7+
8+/**
9+ * タプル
10+ *
11+ * @author Tadashi Nakayama
12+ * @version 1.0.0
13+ * @param <T> ジェネリックス
14+ */
15+public final class Tuple<T> implements Iterable<T>, Serializable {
16+ /** serialVersionUID */
17+ private static final long serialVersionUID = 5818916983288475733L;
18+
19+ /** 値保持配列 */
20+ private final T[] value;
21+
22+ /**
23+ * コンストラクタ
24+ *
25+ * @param vals 値
26+ */
27+ @SafeVarargs
28+ public Tuple(final T... vals) {
29+ if (vals == null) {
30+ throw new IllegalArgumentException();
31+ }
32+
33+ this.value = Arrays.copyOf(vals, vals.length);
34+ }
35+
36+ /**
37+ * 長さ取得
38+ *
39+ * @return 長さ
40+ */
41+ public int length() {
42+ return this.value.length;
43+ }
44+
45+ /**
46+ * 取得
47+ *
48+ * @param loc 位置
49+ * @return 値
50+ */
51+ public T get(final int loc) {
52+ if (loc < 0 || this.value.length <= loc) {
53+ throw new ArrayIndexOutOfBoundsException(loc);
54+ }
55+ return this.value[loc];
56+ }
57+
58+ /**
59+ * @see java.lang.Object#toString()
60+ */
61+ @Override
62+ public String toString() {
63+ return Arrays.toString(this.value);
64+ }
65+
66+ /**
67+ * 配列化
68+ *
69+ * @return 配列
70+ */
71+ public T[] toArray() {
72+ return Arrays.copyOf(this.value, this.value.length);
73+ }
74+
75+ /**
76+ * @see java.lang.Iterable#iterator()
77+ */
78+ @Override
79+ public Iterator<T> iterator() {
80+ return new TupleIterator<>(this.value);
81+ }
82+
83+ /**
84+ * 連結
85+ *
86+ * @param <T> ジェネリックス
87+ * @param val 値
88+ * @param tpl タプル
89+ * @return 連結結果
90+ */
91+ public static <T> Tuple<T> cons(final T[] val, final Tuple<T> tpl) {
92+ return cons(new Tuple<>(val), tpl);
93+ }
94+
95+ /**
96+ * 連結
97+ *
98+ * @param <T> ジェネリックス
99+ * @param tpl タプル
100+ * @param val 値
101+ * @return 連結結果
102+ */
103+ @SafeVarargs
104+ public static <T> Tuple<T> cons(final Tuple<T> tpl, final T... val) {
105+ return cons(tpl, new Tuple<>(val));
106+ }
107+
108+ /**
109+ * 連結
110+ *
111+ * @param <T> ジェネリックス
112+ * @param tpl1 タプル
113+ * @param tpl2 タプル
114+ * @return 連結結果
115+ */
116+ public static <T> Tuple<T> cons(final Tuple<T> tpl1, final Tuple<T> tpl2) {
117+ T[] array = Arrays.copyOf(tpl1.value, tpl1.value.length + tpl2.value.length);
118+ System.arraycopy(tpl2.value, 0, array, tpl1.length(), tpl2.length());
119+ return new Tuple<>(array);
120+ }
121+
122+ /**
123+ * イテレータクラス
124+ *
125+ * @author Tadashi Nakayama
126+ * @version 1.0.0
127+ * @param <E> ジェネリックス
128+ */
129+ private static final class TupleIterator<E> implements Iterator<E> {
130+ /** 現在位置 */
131+ private int loc = 0;
132+ /** インスタンス */
133+ private final E[] value;
134+
135+ /**
136+ * コンストラクタ
137+ *
138+ * @param val インスタンス
139+ */
140+ public TupleIterator(final E[] val) {
141+ this.value = val;
142+ }
143+
144+ /**
145+ * @see java.util.Iterator#hasNext()
146+ */
147+ @Override
148+ public boolean hasNext() {
149+ return this.loc < this.value.length;
150+ }
151+
152+ /**
153+ * @see java.util.Iterator#next()
154+ */
155+ @Override
156+ public E next() {
157+ if (this.value.length <= this.loc) {
158+ throw new NoSuchElementException();
159+ }
160+
161+ return this.value[this.loc++];
162+ }
163+
164+ /**
165+ * @see java.util.Iterator#remove()
166+ */
167+ @Override
168+ public void remove() {
169+ throw new UnsupportedOperationException();
170+ }
171+ }
172+}