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」のソースです。全ソースは最後の公式サイトからダウンロードできます。

  1. /*
  2. * Copyright the original author or authors.
  3. *
  4. * Licensed under the MOZILLA PUBLIC LICENSE, Version 1.1 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. *      http://www.mozilla.org/MPL/MPL-1.1.html
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package org.idmedia.as3commons.util {
  17.   import org.idmedia.as3commons.lang.NullPointerException;
  18.  
  19.   /**
  20.    * Hash table based implementation of the <tt>Map</tt> interface.  This
  21.    * implementation provides all of the optional map operations, and permits
  22.    * <tt>null</tt> values and the <tt>null</tt> key.
  23.    * This class makes no guarantees as to the order of the map; in particular,
  24.    * it does not guarantee that the order will remain constant over time.
  25.    *
  26.    * <p>This implementation provides constant-time performance for the basic
  27.    * operations (<tt>get</tt> and <tt>put</tt>), assuming the hash function
  28.    * disperses the elements properly among the buckets.  Iteration over
  29.    * collection views requires time proportional to the "capacity" of the
  30.    * <tt>HashMap</tt> instance (the number of buckets) plus its size (the number
  31.    * of key-value mappings).
  32.    *
  33.    * <p>The iterators returned by all of this class's "collection view methods"
  34.    * are <i>fail-fast</i>: if the map is structurally modified at any time after
  35.    * the iterator is created, in any way except through the iterator's own
  36.    * <tt>remove</tt> or <tt>add</tt> methods, the iterator will throw a
  37.    * <tt>ConcurrentModificationException</tt>.  Thus, in the face of concurrent
  38.    * modification, the iterator fails quickly and cleanly, rather than risking
  39.    * arbitrary, non-deterministic behavior at an undetermined time in the
  40.    * future.
  41.    *
  42.    * <p>Note that the fail-fast behavior of an iterator cannot be guaranteed
  43.    * as it is, generally speaking, impossible to make any hard guarantees in the
  44.    * presence of unsynchronized concurrent modification.  Fail-fast iterators
  45.    * throw <tt>ConcurrentModificationException</tt> on a best-effort basis.
  46.    * Therefore, it would be wrong to write a program that depended on this
  47.    * exception for its correctness: <i>the fail-fast behavior of iterators
  48.    * should be used only to detect bugs.</i>
  49.    *
  50.    * @author sleistner
  51.    * @inheritDoc
  52.    */
  53.   public class HashMap extends AbstractMap implements Map {
  54.    
  55.     private var entries:Set;
  56.    
  57.     /**
  58.      * Constructs an empty <tt>HashMap</tt>
  59.      */
  60.     function HashMap() {
  61.       entries = new EntrySet();
  62.     }
  63.    
  64.     /**
  65.      * Returns a collection view of the mappings contained in this map.  Each
  66.      * element in the returned collection is a <tt>Entry</tt>.  The
  67.      * collection is backed by the map, so changes to the map are reflected in
  68.      * the collection, and vice-versa.  The collection supports element
  69.      * removal, which removes the corresponding mapping from the map, via the
  70.      * <tt>Iterator.remove</tt>, <tt>Collection.remove</tt>,
  71.      * <tt>removeAll</tt>, <tt>retainAll</tt>, and <tt>clear</tt> operations.
  72.      * It does not support the <tt>add</tt> or <tt>addAll</tt> operations.
  73.      *
  74.      * @return a collection view of the mappings contained in this map.
  75.      * @see Entry Entry
  76.      */
  77.     override public function entrySet():Set {
  78.       return entries;   
  79.     }
  80.    
  81.     /**
  82.      * Associates the specified value with the specified key in this map.
  83.      * If the map previously contained a mapping for this key, the old
  84.      * value is replaced.
  85.      *
  86.      * @param key key with which the specified value is to be associated.
  87.      * @param value value to be associated with the specified key.
  88.      * @return previous value associated with specified key, or <tt>null</tt>
  89.      *           if there was no mapping for key.  A <tt>null</tt> return can
  90.      *           also indicate that the HashMap previously associated
  91.      *           <tt>null</tt> with the specified key.
  92.      */
  93.     override public function put(key:*, value:*):* {
  94.       if(key == null) {
  95.         throw new NullPointerException();
  96.       }
  97.  
  98.       var iter:Iterator = entries.iterator();
  99.       while(iter.hasNext()) {
  100.         var e:Entry = Entry(iter.next());
  101.         if(e.getKey() === key) {
  102.           var oldValue:* = e.getValue();
  103.           e.setValue(value);
  104.           return oldValue;   
  105.         }   
  106.       }
  107.       entries.add(new EntryImpl(key, value));
  108.       return null;
  109.     }
  110.   }
  111. }
  112.  
  113. import org.idmedia.as3commons.lang.IllegalArgumentException;
  114. import org.idmedia.as3commons.lang.IllegalStateException;
  115. import org.idmedia.as3commons.lang.NoSuchElementException;
  116. import org.idmedia.as3commons.util.AbstractSet;
  117. import org.idmedia.as3commons.util.Entry;
  118. import org.idmedia.as3commons.util.Iterator;
  119.  
  120. internal class EntryImpl implements Entry {
  121.  
  122.   private var key:*;
  123.   private var value:*;
  124.  
  125.   function EntryImpl(key:*, value:*) {
  126.     this.key = key;
  127.     this.value = value;
  128.   }
  129.  
  130.   public function getKey():* {
  131.     return key;
  132.   }
  133.  
  134.   public function getValue():* {
  135.     return value;
  136.   }
  137.  
  138.   public function setValue(newValue:*):* {
  139.     var oldValue:* = value;
  140.     value = newValue;
  141.     return oldValue;
  142.   }
  143.  
  144.   public function equals(o:*):Boolean {
  145.     return o === this;
  146.   }
  147. }
  148.  
  149. internal class EntrySet extends AbstractSet {
  150.  
  151.   private var table:Array;
  152.   private var tableSize:int;
  153.  
  154.   function EntrySet() {
  155.     table = new Array();
  156.     tableSize = 0;
  157.   }
  158.  
  159.   override public function iterator():Iterator {
  160.     return new EntrySetIterator(this);   
  161.   }
  162.  
  163.   override public function add(object:*):Boolean {
  164.     if(!(object is Entry)) {
  165.       throw new IllegalArgumentException();   
  166.     }
  167.  
  168.     if(!contains(object)) {
  169.       table.push(object);
  170.       tableSize++;
  171.       return true;   
  172.     }
  173.     return false;
  174.   }
  175.  
  176.   override public function remove(entry:* = null):Boolean {
  177.     for(var i:int = 0;i < tableSize; i++) {
  178.       if(Entry(entry).equals(table[i])) {
  179.         table.splice(i, 1);
  180.         tableSize--;
  181.         return true;   
  182.       }   
  183.     }
  184.     return false;
  185.   }
  186.  
  187.   public function get(index:int):* {
  188.     return table[index];
  189.   }
  190.  
  191.   public function removeEntryForKey(key:*):Entry {
  192.     var e:Entry = null;
  193.     for(var i:int = 0;i < tableSize; i++) {
  194.       if(Entry(table[i]).getKey() === key) {
  195.         e = table[i];
  196.         table.splice(i, 1);
  197.         tableSize--;
  198.         return e;   
  199.       }   
  200.     }
  201.     return e;
  202.   }
  203.  
  204.   override public function size():int {
  205.     return tableSize;   
  206.   }
  207. }
  208.  
  209. internal class EntrySetIterator implements Iterator {
  210.  
  211.   private var cursor:int = 0;
  212.   private var current:Entry = null;
  213.   private var s:EntrySet = null;
  214.  
  215.   function EntrySetIterator(s:EntrySet = null) {
  216.     this.s = s;
  217.   }
  218.  
  219.   public function hasNext():Boolean {
  220.     return (cursor < s.size());
  221.   }
  222.  
  223.   public function next():* {
  224.     var h:int = s.size();
  225.     current = s.get(cursor++) as Entry;
  226.     if(current == null) {
  227.       throw new NoSuchElementException();   
  228.     }
  229.     return current;
  230.   }
  231.  
  232.   public function remove():void {
  233.     if(current == null) {
  234.       throw new IllegalStateException();   
  235.     }
  236.     var key:* = current.getKey();
  237.     current = null;
  238.     s.removeEntryForKey(key);
  239.   }
  240. }

参考リソース

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

メインコンテンツEND ■
1 Star2 Stars3 Stars4 Stars5 Stars (No Ratings Yet)
Loading ... Loading ...

Posted on Thursday, 26th February 2009 by admin

Tags: , , ,
Posted in ActionScript, Flash Project | Comments (1) | 2,439 views

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

  1. CASA Lib: 柔軟なActionScript共通基盤としてオープンソースのライブラリ | DigiTechLog Dot Com Says:

    [...] 「CASA Lib」とは、紹介した「AS3Commons: java.(lang|util).*みたいのActionScript 3用のオープンソースユーティリティ共通関数集合」が似てる柔軟なActionScript共通基盤として、開発の効率をアップで [...]

Leave a Reply