AS3Commons: java.(lang|util).*みたいのActionScript 3用のオープンソースユーティリティ共通関数集合

AS3Commonsとは、java.(lang|util).*のようのActionScript 3用のオープンソースユーティリティ共通関数集合です。有名な同様のActionScriptのライブラリは「as3corelib」というものがあります(as3corelibに興味がある方は次のURLでas3corelibクラス単位のトライアルを参照できる)。リリースされている「AS3Commons」のソースを見ると構造はやっはりJavaのlangとutilと大体同じと感じです。作者たち(現時点までは4人がいる)はそれを基づいて進めると思います。

配列の「Arrays」や「HashMap」のようのクラスも含めてますので、AS3の初心者に対してはいい教材ですよね。ライセンスは商用でもやさしい「Mozilla Public License 1.1 (MPL 1.1)」です。

以下は「HashMap.as」のソースです。全ソースは最後の公式サイトからダウンロードできます。


/*
* Copyright the original author or authors.
*
* Licensed under the MOZILLA PUBLIC LICENSE, Version 1.1 (the “License”);
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.mozilla.org/MPL/MPL-1.1.html
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an “AS IS” BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.idmedia.as3commons.util {
import org.idmedia.as3commons.lang.NullPointerException;

/**
* Hash table based implementation of the Map interface. This
* implementation provides all of the optional map operations, and permits
* null values and the null key.
* This class makes no guarantees as to the order of the map; in particular,
* it does not guarantee that the order will remain constant over time.
*
*

This implementation provides constant-time performance for the basic
* operations (get and put), assuming the hash function
* disperses the elements properly among the buckets. Iteration over
* collection views requires time proportional to the “capacity” of the
* HashMap instance (the number of buckets) plus its size (the number
* of key-value mappings).
*
*

The iterators returned by all of this class’s “collection view methods”
* are fail-fast: if the map is structurally modified at any time after
* the iterator is created, in any way except through the iterator’s own
* remove or add methods, the iterator will throw a
* ConcurrentModificationException. Thus, in the face of concurrent
* modification, the iterator fails quickly and cleanly, rather than risking
* arbitrary, non-deterministic behavior at an undetermined time in the
* future.
*
*

Note that the fail-fast behavior of an iterator cannot be guaranteed
* as it is, generally speaking, impossible to make any hard guarantees in the
* presence of unsynchronized concurrent modification. Fail-fast iterators
* throw ConcurrentModificationException on a best-effort basis.
* Therefore, it would be wrong to write a program that depended on this
* exception for its correctness: the fail-fast behavior of iterators
* should be used only to detect bugs.

*
* @author sleistner
* @inheritDoc
*/
public class HashMap extends AbstractMap implements Map {

private var entries:Set;

/**
* Constructs an empty HashMap
*/
function HashMap() {
entries = new EntrySet();
}

/**
* Returns a collection view of the mappings contained in this map. Each
* element in the returned collection is a Entry. The
* collection is backed by the map, so changes to the map are reflected in
* the collection, and vice-versa. The collection supports element
* removal, which removes the corresponding mapping from the map, via the
* Iterator.remove, Collection.remove,
* removeAll, retainAll, and clear operations.
* It does not support the add or addAll operations.
*
* @return a collection view of the mappings contained in this map.
* @see Entry Entry
*/
override public function entrySet():Set {
return entries;
}

/**
* Associates the specified value with the specified key in this map.
* If the map previously contained a mapping for this key, the old
* value is replaced.
*
* @param key key with which the specified value is to be associated.
* @param value value to be associated with the specified key.
* @return previous value associated with specified key, or null
* if there was no mapping for key. A null return can
* also indicate that the HashMap previously associated
* null with the specified key.
*/
override public function put(key:*, value:*):* {
if(key == null) {
throw new NullPointerException();
}

var iter:Iterator = entries.iterator();
while(iter.hasNext()) {
var e:Entry = Entry(iter.next());
if(e.getKey() === key) {
var oldValue:* = e.getValue();
e.setValue(value);
return oldValue;
}
}
entries.add(new EntryImpl(key, value));
return null;
}
}
}

import org.idmedia.as3commons.lang.IllegalArgumentException;
import org.idmedia.as3commons.lang.IllegalStateException;
import org.idmedia.as3commons.lang.NoSuchElementException;
import org.idmedia.as3commons.util.AbstractSet;
import org.idmedia.as3commons.util.Entry;
import org.idmedia.as3commons.util.Iterator;

internal class EntryImpl implements Entry {

private var key:*;
private var value:*;

function EntryImpl(key:*, value:*) {
this.key = key;
this.value = value;
}

public function getKey():* {
return key;
}

public function getValue():* {
return value;
}

public function setValue(newValue:*):* {
var oldValue:* = value;
value = newValue;
return oldValue;
}

public function equals(o:*):Boolean {
return o === this;
}
}

internal class EntrySet extends AbstractSet {

private var table:Array;
private var tableSize:int;

function EntrySet() {
table = new Array();
tableSize = 0;
}

override public function iterator():Iterator {
return new EntrySetIterator(this);
}

override public function add(object:*):Boolean {
if(!(object is Entry)) {
throw new IllegalArgumentException();
}

if(!contains(object)) {
table.push(object);
tableSize++;
return true;
}
return false;
}

override public function remove(entry:* = null):Boolean {
for(var i:int = 0;i < tableSize; i++) { if(Entry(entry).equals(table[i])) { table.splice(i, 1); tableSize--; return true; } } return false; } public function get(index:int):* { return table[index]; } public function removeEntryForKey(key:*):Entry { var e:Entry = null; for(var i:int = 0;i < tableSize; i++) { if(Entry(table[i]).getKey() === key) { e = table[i]; table.splice(i, 1); tableSize--; return e; } } return e; } override public function size():int { return tableSize; } } internal class EntrySetIterator implements Iterator { private var cursor:int = 0; private var current:Entry = null; private var s:EntrySet = null; function EntrySetIterator(s:EntrySet = null) { this.s = s; } public function hasNext():Boolean { return (cursor < s.size()); } public function next():* { var h:int = s.size(); current = s.get(cursor++) as Entry; if(current == null) { throw new NoSuchElementException(); } return current; } public function remove():void { if(current == null) { throw new IllegalStateException(); } var key:* = current.getKey(); current = null; s.removeEntryForKey(key); } }

参考リソース

http://sourceforge.net/projects/as3commons 公式サイト

1 thought on “AS3Commons: java.(lang|util).*みたいのActionScript 3用のオープンソースユーティリティ共通関数集合”

Leave a Reply

Your email address will not be published. Required fields are marked *