001: // THIS SOFTWARE IS PROVIDED BY SOFTARIS PTY.LTD. AND OTHER METABOSS
002: // CONTRIBUTORS ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING,
003: // BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
004: // FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SOFTARIS PTY.LTD.
005: // OR OTHER METABOSS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
006: // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
007: // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
008: // OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
009: // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
010: // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
011: // EVEN IF SOFTARIS PTY.LTD. OR OTHER METABOSS CONTRIBUTORS ARE ADVISED OF THE
012: // POSSIBILITY OF SUCH DAMAGE.
013: //
014: // Copyright 2000-2005 © Softaris Pty.Ltd. All Rights Reserved.
015: package com.metaboss.util;
016:
017: import java.io.ByteArrayInputStream;
018: import java.io.ByteArrayOutputStream;
019: import java.io.IOException;
020: import java.io.ObjectInputStream;
021: import java.io.ObjectOutputStream;
022:
023: /** Set of useful utilites to do with objects */
024: public class ObjectUtils {
025: /** This method deals with null references and than just calls
026: * standard equals method. Two nulls are considered equal */
027: public static boolean equals(Object pObject1, Object pObject2) {
028: if (pObject1 == null && pObject2 == null)
029: return true;
030: if (pObject1 == null || pObject2 == null)
031: return false;
032: return pObject1.equals(pObject2);
033: }
034:
035: /** This helper method tests if given object is an instance of any of the
036: * given classes.
037: * @param pObjectToTest the instance being tested
038: * @param pTypesToMatch the types to thest the instance against
039: * @return true if given object is an instance of one of the given types
040: */
041: public static boolean isInstance(Object pObjectToTest,
042: Class[] pTypesToMatch) {
043: for (int i = 0; i < pTypesToMatch.length; i++) {
044: if (pTypesToMatch[i].isInstance(pObjectToTest))
045: return true;
046: }
047: return false;
048: }
049:
050: /** This helper method tests if given object array is full.
051: * Full array has non zero length and every element of the array initilised
052: * to something other than null
053: * @param pArrayToTest the array being tested
054: * @return true if given object aray is full
055: */
056: public static boolean isFullArray(Object[] pArrayToTest) {
057: if (pArrayToTest == null || pArrayToTest.length == 0)
058: return false;
059: for (int i = 0; i < pArrayToTest.length; i++) {
060: if (pArrayToTest[i] == null)
061: return false; // Found null element
062: }
063: return true; // non zero length array wheere every element is no null - array is full
064: }
065:
066: /** This helper method converts object into string representing binary dump of object. It will only work
067: * successfully on serializeable objects */
068: public static String pack(Object pSourceObject) throws IOException {
069: if (pSourceObject == null)
070: return null;
071: ByteArrayOutputStream lOstream = null;
072: ObjectOutputStream lOostream = null;
073: try {
074: lOstream = new ByteArrayOutputStream();
075: lOostream = new ObjectOutputStream(lOstream);
076: lOostream.writeObject(pSourceObject);
077: lOostream.flush();
078: lOstream.flush();
079: return ByteUtils.bytesToText(lOstream.toByteArray());
080: } finally {
081: if (lOostream != null)
082: lOostream.close();
083: if (lOstream != null)
084: lOstream.close();
085: }
086: }
087:
088: /** This helper method converts object stored in the source string with binary dump of object into coresponding java object. */
089: public static Object unpack(String pSourceBuffer)
090: throws IOException, ClassNotFoundException {
091: if (pSourceBuffer == null)
092: return null;
093: ByteArrayInputStream lIstream = null;
094: ObjectInputStream lIistream = null;
095: try {
096: lIstream = new ByteArrayInputStream(ByteUtils
097: .textToBytes(pSourceBuffer));
098: lIistream = new ObjectInputStream(lIstream);
099: return lIistream.readObject();
100: } finally {
101: if (lIistream != null)
102: lIistream.close();
103: if (lIstream != null)
104: lIstream.close();
105: }
106: }
107: }
|