Skip to content

Commit 3634575

Browse files
committed
✨ Stream関連に型を付与
1 parent b63d3a2 commit 3634575

20 files changed

+799
-306
lines changed

dist/JavaLibraryScript.js

Lines changed: 315 additions & 44 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/JavaLibraryScript.js.map

Lines changed: 13 additions & 11 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/JavaLibraryScript.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/JavaLibraryScript.min.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "javalibraryscript",
3-
"version": "v1.0.4.5",
3+
"version": "v1.0.4.6",
44
"description": "Javaの機能をJavaScriptに適当に再現",
55
"main": "index.js",
66
"scripts": {

src/base/Interface.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -123,11 +123,13 @@ class Interface extends JavaLibraryScriptCore {
123123
static convert(TargetClass, newDefs = {}, { inherit = true, abstract = true } = {}) {
124124
this.applyTo(TargetClass, newDefs, { inherit });
125125

126+
const this_ = this;
127+
126128
const interfaceClass = class extends TargetClass {
127129
constructor(...args) {
128130
if (abstract) {
129131
if (new.target === interfaceClass) {
130-
this._handleError(TypeError, `Cannot instantiate abstract class ${TargetClass.name}`);
132+
new TypeError(`Cannot instantiate abstract class ${TargetClass.name}`);
131133
}
132134
}
133135
super(...args);
@@ -154,7 +156,7 @@ class Interface extends JavaLibraryScriptCore {
154156
const expectedArgs = def.args || [];
155157
for (let i = 0; i < expectedArgs.length; i++) {
156158
if (!TypeChecker.matchType(args[i], expectedArgs[i])) {
157-
this._handleError(TypeError, `"${this.constructor.name}.${methodName}" 第${i + 1}引数: ${TypeChecker.typeNames(expectedArgs[i])} を期待 → 実際: ${TypeChecker.stringify(args[i])}`);
159+
this_._handleError(TypeError, `"${this.constructor.name}.${methodName}" 第${i + 1}引数: ${TypeChecker.typeNames(expectedArgs[i])} を期待 → 実際: ${TypeChecker.stringify(args[i])}`);
158160
}
159161
}
160162

@@ -164,9 +166,9 @@ class Interface extends JavaLibraryScriptCore {
164166
const validate = (val) => {
165167
if (!TypeChecker.matchType(val, expectedReturn)) {
166168
if (expectedReturn === TypeChecker.NoReturn) {
167-
this._handleError(TypeError, `"${this.constructor.name}.${methodName}" は戻り値を返してはいけません → 実際: ${TypeChecker.stringify(val)}`);
169+
this_._handleError(TypeError, `"${this.constructor.name}.${methodName}" は戻り値を返してはいけません → 実際: ${TypeChecker.stringify(val)}`);
168170
} else {
169-
this._handleError(TypeError, `"${this.constructor.name}.${methodName}" の戻り値: ${TypeChecker.typeNames(expectedReturn)} を期待 → 実際: ${TypeChecker.stringify(val)}`);
171+
this_._handleError(TypeError, `"${this.constructor.name}.${methodName}" の戻り値: ${TypeChecker.typeNames(expectedReturn)} を期待 → 実際: ${TypeChecker.stringify(val)}`);
170172
}
171173
}
172174
return val;

src/util/HashMap.js

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
const { TypeChecker } = require("../libs");
12
const MapInterface = require("./MapInterface");
23
const EntryStream = require("./stream/EntryStream.js");
34

@@ -151,7 +152,7 @@ class HashMap extends MapInterface {
151152
* @returns {boolean}
152153
*/
153154
equals(otherMap) {
154-
if (this.size !== otherMap.size) return false;
155+
if (!(otherMap instanceof Map) || this.size !== otherMap.size) return false;
155156
for (const [k, v] of this.entries()) {
156157
if (!otherMap.has(k) || otherMap.get(k) !== v) return false;
157158
}
@@ -190,18 +191,15 @@ class HashMap extends MapInterface {
190191
* @returns {string}
191192
*/
192193
toString() {
193-
const data = Array.from(this.entries())
194-
.map(([k, v]) => `${k}=${v}`)
195-
.join(", ");
196-
return `{ ${data} }`;
194+
return `${this.constructor.name}<${TypeChecker.typeNames(this._KeyType)}, ${TypeChecker.typeNames(this._ValueType)}>(size=${this.size})`;
197195
}
198196

199197
/**
200198
* イテレータを返却する
201199
* @returns {Iterator<V>}
202200
*/
203201
[Symbol.iterator]() {
204-
return this.entries()[Symbol.iterator]();
202+
return this.entries();
205203
}
206204
}
207205

src/util/HashSet.js

Lines changed: 210 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,210 @@
1+
const SetInterface = require("./SetInterface");
2+
const TypeChecker = require("../libs/TypeChecker");
3+
const StreamChecker = require("./stream/StreamChecker");
4+
const Stream = require("./stream/Stream.js");
5+
6+
/**
7+
* 型チェック機能のついたMap
8+
* @template V
9+
* @extends {SetInterface<V>}
10+
* @class
11+
*/
12+
class HashSet extends SetInterface {
13+
/**
14+
* @param {Function} ValueType
15+
*/
16+
constructor(ValueType) {
17+
super(ValueType);
18+
}
19+
20+
// ==================================================
21+
// 基本操作(override)
22+
// ==================================================
23+
24+
/**
25+
* 値を追加する
26+
* @param {V} value
27+
* @returns {this}
28+
* @throws {TypeError}
29+
*/
30+
add(value) {
31+
this._checkValue(value);
32+
return super.add(value);
33+
}
34+
35+
/**
36+
* 値を一括で追加する
37+
* @param {Iterable<V>} collection
38+
* @returns {this}
39+
* @throws {TypeError}
40+
*/
41+
addAll(collection) {
42+
for (const item of collection) {
43+
this.add(item);
44+
}
45+
return this;
46+
}
47+
48+
/**
49+
* 値の存在を確認
50+
* @param {V} value
51+
* @returns {boolean}
52+
* @throws {TypeError}
53+
*/
54+
has(value) {
55+
this._checkValue(value);
56+
return super.has(value);
57+
}
58+
/**
59+
* 値の存在を確認
60+
* @param {V} value
61+
* @returns {boolean}
62+
* @throws {TypeError}
63+
*/
64+
contains(value) {
65+
return this.has(value);
66+
}
67+
68+
/**
69+
* 全ての値の存在を確認
70+
* @param {Iterable<V>} collection
71+
* @returns {boolean}
72+
* @throws {TypeError}
73+
*/
74+
containsAll(collection) {
75+
for (const item of collection) {
76+
if (!this.has(item)) return false;
77+
}
78+
return true;
79+
}
80+
81+
/**
82+
* 値を削除する
83+
* @param {V} value
84+
* @returns {boolean}
85+
* @throws {TypeError}
86+
*/
87+
delete(value) {
88+
this._checkValue(value);
89+
return super.delete(value);
90+
}
91+
/**
92+
* 値を削除する
93+
* @param {V} value
94+
* @returns {boolean}
95+
* @throws {TypeError}
96+
*/
97+
remove(value) {
98+
return this.delete(value);
99+
}
100+
101+
/**
102+
* 全ての値を削除する
103+
* @param {Iterable<V>} collection
104+
* @returns {boolean}
105+
* @throws {TypeError}
106+
*/
107+
removeAll(collection) {
108+
let modified = false;
109+
for (const item of collection) {
110+
modified = this.delete(item) || modified;
111+
}
112+
return modified;
113+
}
114+
115+
/**
116+
* 空かどうかを返却する
117+
* @returns {boolean}
118+
*/
119+
isEmpty() {
120+
return this.size === 0;
121+
}
122+
123+
/**
124+
* 含まれない要素を全削除する
125+
* @param {Iterable<V>} collection
126+
* @returns {boolean}
127+
* @throws {TypeError}
128+
*/
129+
retainAll(collection) {
130+
const otherSet = new Set(collection);
131+
let modified = false;
132+
for (const item of this) {
133+
if (!otherSet.has(item)) {
134+
this.delete(item);
135+
modified = true;
136+
}
137+
}
138+
return modified;
139+
}
140+
141+
// ==================================================
142+
// 追加機能
143+
// ==================================================
144+
145+
/**
146+
* 等価判定を行う
147+
* @param {this} otherSet
148+
* @returns {boolean}
149+
*/
150+
equals(otherSet) {
151+
if (!(otherSet instanceof Set) || this.size !== otherSet.size) return false;
152+
for (const item of this) {
153+
if (!otherSet.has(item)) return false;
154+
}
155+
return true;
156+
}
157+
158+
/**
159+
* 全てのデータを呼び出す
160+
* @param {Function} callback
161+
* @param {any} [thisArg]
162+
*/
163+
forEach(callback, thisArg) {
164+
for (const item of this) {
165+
callback.call(thisArg, item, item, this);
166+
}
167+
}
168+
169+
// ==================================================
170+
// Stream
171+
// ==================================================
172+
173+
/**
174+
* Streamを返却する
175+
* @returns {Stream<V>}
176+
*/
177+
stream() {
178+
return StreamChecker.typeToStream(this._ValueType).from(this.values(), this._ValueType);
179+
}
180+
181+
// ==================================================
182+
// 基本操作(システム)
183+
// ==================================================
184+
185+
/**
186+
* 配列に変換する
187+
* @returns {V[]}
188+
*/
189+
toArray() {
190+
return Array.from(this);
191+
}
192+
193+
/**
194+
* 文字列に変換する
195+
* @returns {string}
196+
*/
197+
toString() {
198+
return `${this.constructor.name}<${TypeChecker.typeNames(this._ValueType)}>(size=${this.size})`;
199+
}
200+
201+
/**
202+
* イテレータを返却する
203+
* @returns {Iterator<V>}
204+
*/
205+
[Symbol.iterator]() {
206+
return this.values();
207+
}
208+
}
209+
210+
module.exports = HashSet;

src/util/MapInterface.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@ class MapInterface extends Map {
2323
*/
2424
constructor(KeyType, ValueType) {
2525
super();
26-
this._KeyType = KeyType;
27-
this._ValueType = ValueType;
26+
this._KeyType = KeyType || Any;
27+
this._ValueType = ValueType || Any;
2828
}
2929

3030
/**

src/util/SetInterface.js

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ class SetInterface extends Set {
2222
*/
2323
constructor(ValueType) {
2424
super();
25-
this._ValueType = ValueType;
25+
this._ValueType = ValueType || Any;
2626
}
2727

2828
/**
@@ -38,12 +38,11 @@ class SetInterface extends Set {
3838
}
3939

4040
module.exports = Interface.convert(SetInterface, {
41-
set: { args: [NotEmpty, NotEmpty], returns: Any },
42-
put: { args: [NotEmpty, NotEmpty], returns: Any },
41+
add: { args: [NotEmpty], returns: Any },
4342
delete: { args: [NotEmpty], returns: Boolean },
4443
remove: { args: [NotEmpty], returns: Boolean },
4544
isEmpty: { returns: Boolean },
4645
clear: { returns: NoReturn },
4746
has: { args: [NotEmpty], returns: Boolean },
48-
containsValue: { args: [NotEmpty], returns: Boolean },
47+
contains: { args: [NotEmpty], returns: Boolean },
4948
});

0 commit comments

Comments
 (0)