| 1 |
/* |
|---|
| 2 |
* Copyright(c) 2006-2007 the Spark project. |
|---|
| 3 |
* |
|---|
| 4 |
* Licensed under the Apache License, Version 2.0 (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.apache.org/licenses/LICENSE-2.0 |
|---|
| 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, |
|---|
| 13 |
* either express or implied. See the License for the specific language |
|---|
| 14 |
* governing permissions and limitations under the License. |
|---|
| 15 |
*/ |
|---|
| 16 |
|
|---|
| 17 |
|
|---|
| 18 |
package org.libspark.utils |
|---|
| 19 |
{ |
|---|
| 20 |
|
|---|
| 21 |
/** |
|---|
| 22 |
* オブジェクトのためのユーティリティクラスです |
|---|
| 23 |
*/ |
|---|
| 24 |
public class ObjectUtil |
|---|
| 25 |
{ |
|---|
| 26 |
|
|---|
| 27 |
/** |
|---|
| 28 |
* オブジェクトに指定された複数個のプロパティがすべて定義されているかどうかを示します。 |
|---|
| 29 |
* |
|---|
| 30 |
* @param target 対象オブジェクト |
|---|
| 31 |
* @param ...propNames オブジェクトのプロパティ名(型はすべて String です) |
|---|
| 32 |
* @return 指定されたプロパティがすべて定義されている場合は true を返します。それ以外の場合は false になります。 |
|---|
| 33 |
* @author michi at seyself.com |
|---|
| 34 |
*/ |
|---|
| 35 |
public static function hasOwnProperties( target:Object, ...propNames/* of String */ ):Boolean |
|---|
| 36 |
{ |
|---|
| 37 |
var len:uint = propNames.length; |
|---|
| 38 |
for ( var i:uint = 0; i < len;i++ ) { |
|---|
| 39 |
if ( !target.hasOwnProperty(propNames[i]) ) return false; |
|---|
| 40 |
} |
|---|
| 41 |
return true; |
|---|
| 42 |
} |
|---|
| 43 |
|
|---|
| 44 |
/** |
|---|
| 45 |
* ループ処理で列挙可能なプロパティの総数を調べます |
|---|
| 46 |
* |
|---|
| 47 |
* @param target 対象オブジェクト |
|---|
| 48 |
* @return ループ処理で列挙可能なプロパティの総数 |
|---|
| 49 |
* @author michi at seyself.com |
|---|
| 50 |
*/ |
|---|
| 51 |
public static function propLength( target:Object ):uint |
|---|
| 52 |
{ |
|---|
| 53 |
var n:uint = 0; |
|---|
| 54 |
for (var val:String in target) n++; |
|---|
| 55 |
return n; |
|---|
| 56 |
} |
|---|
| 57 |
|
|---|
| 58 |
/** |
|---|
| 59 |
* オブジェクトのプロパティを配列にします。 |
|---|
| 60 |
* 引数にプロパティ名を指定した場合、指定された順に配列を構成します。 |
|---|
| 61 |
* プロパティ名の指定がない場合は for ループで参照した順に構成します。 |
|---|
| 62 |
* |
|---|
| 63 |
* @param target 対象オブジェクト |
|---|
| 64 |
* @param ...propNames プロパティ名を文字列で指定します |
|---|
| 65 |
* @return |
|---|
| 66 |
* @author michi at seyself.com |
|---|
| 67 |
*/ |
|---|
| 68 |
public static function toArray(target:Object, propNames:Array=null /* of String */ ):Array |
|---|
| 69 |
{ |
|---|
| 70 |
var a:Array = []; |
|---|
| 71 |
if ( propNames ) { |
|---|
| 72 |
var len:uint = propNames.length; |
|---|
| 73 |
for (var i:uint = 0; i < len; i++ ) { |
|---|
| 74 |
a.push( target[propNames[i]] ); |
|---|
| 75 |
} |
|---|
| 76 |
return a; |
|---|
| 77 |
} else { |
|---|
| 78 |
for (var val:String in target) { |
|---|
| 79 |
a.push(target[val]); |
|---|
| 80 |
} |
|---|
| 81 |
return a; |
|---|
| 82 |
} |
|---|
| 83 |
} |
|---|
| 84 |
|
|---|
| 85 |
/** |
|---|
| 86 |
* オブジェクトが持つ列挙可能なプロパティの名前を一覧で取得します。 |
|---|
| 87 |
* |
|---|
| 88 |
* @param target オブジェクト |
|---|
| 89 |
* @return プロパティ名の一覧 |
|---|
| 90 |
* @author michi at seyself.com |
|---|
| 91 |
*/ |
|---|
| 92 |
public static function getPropNames(target:Object):Array |
|---|
| 93 |
{ |
|---|
| 94 |
var a:Array = []; |
|---|
| 95 |
for (var val:String in target) a.push(val); |
|---|
| 96 |
return a; |
|---|
| 97 |
} |
|---|
| 98 |
|
|---|
| 99 |
/** |
|---|
| 100 |
* オブジェクトが持つ列挙可能なプロパティの値を一覧で取得します。 |
|---|
| 101 |
* |
|---|
| 102 |
* @param target オブジェクト |
|---|
| 103 |
* @return プロパティ値の一覧 |
|---|
| 104 |
* @author michi at seyself.com |
|---|
| 105 |
*/ |
|---|
| 106 |
public static function getPropValues(target:Object):Array |
|---|
| 107 |
{ |
|---|
| 108 |
var a:Array = []; |
|---|
| 109 |
for each(var val:* in target) a.push(val); |
|---|
| 110 |
return a; |
|---|
| 111 |
} |
|---|
| 112 |
|
|---|
| 113 |
} |
|---|
| 114 |
} |
|---|