Click here to Skip to main content
15,886,258 members
Articles / Programming Languages / Java / Java SE

OpenNxSerialization

Rate me:
Please Sign up or sign in to vote.
5.00/5 (1 vote)
16 Jun 2009GPL32 min read 20.4K   177   9  
Speed up object serialization in Java.
/*
 * @(#)NxTypeSurrogateSelectorBase.java	1.0
 *
 * Created on September 18, 2008, 12:59 PM
 *
 * Copyright 2008 NeXtreme Innovations, Inc. All rights reserved.
 * "NeXtreme Innovations" PROPRIETARY/CONFIDENTIAL. Use is subject
 * to license terms.
 */

package com.nextreme.opennxserialization.core.io;

import com.nextreme.opennxserialization.core.NxPackageResources;
import com.nextreme.opennxserialization.core.io.surrogates.NxArgumentException;
import com.nextreme.opennxserialization.core.io.surrogates.NxBuiltinSerializationSurrogate;
import com.nextreme.opennxserialization.core.io.surrogates.NxSerializationSurrogate;
import java.util.HashMap;
import java.util.Map;

/**
 * NxTypeSurrogateSelectorBase class.
 *
 * @version 1.0, September 18, 2008
 */
public class NxTypeSurrogateSelectorBase implements NxTypeSurrogateSelector
{
    /**
     * Minimum id that can be assigned to a user defined type.
     */
    protected NxSerializationSurrogate mNullSurrogate;
    
    /**
     * Minimum id that can be assigned to a user defined type.
     */
    protected NxSerializationSurrogate mDefaultSurrogate;
    
    /**
     * Minimum id that can be assigned to a user defined type.
     */
    protected Map<Class, NxSerializationSurrogate> mTypeSurrogateMap;
    
    /**
     * Minimum id that can be assigned to a user defined type.
     */
    protected Map<Short, NxSerializationSurrogate> mHandleSurrogateMap;
    
    /**
     * Minimum id that can be assigned to a user defined type.
     */
    protected Short mNewTypeHandle;

    /**
     * Creates a new instance of NxTypeSurrogateSelectorBase
     */
    public NxTypeSurrogateSelectorBase() {
        construct(true);
    }
    
    /**
     * Creates a new instance of NxTypeSurrogateSelectorBase
     */
    public NxTypeSurrogateSelectorBase(boolean useBuiltinSurrogates) 
    {
        construct(useBuiltinSurrogates);
    }

    private void construct(boolean useBuiltinSurrogates) 
    {
        mTypeSurrogateMap = new HashMap<Class, NxSerializationSurrogate>();
        mHandleSurrogateMap = new HashMap<Short, NxSerializationSurrogate>();
        mNewTypeHandle = NxTypeSurrogateConstants.MinTypeHandle;
    }

    /*
     * Returns the default type surrogate selector Object
    */
    public NxSerializationSurrogate getNullSurrogate()
    {
        return this.mNullSurrogate;
    }

    /*
     * Returns the default type surrogate selector Object
    */
    public void setNullSurrogate(NxSerializationSurrogate value)
    {
        this.mNullSurrogate = value;
    }
    /*
     * Returns the default type surrogate selector Object
    */
    public NxSerializationSurrogate getDefaultSurrogate()
    {
        return this.mDefaultSurrogate;
    }

    /*
     * Returns the default type surrogate selector Object
    */
    public void setDefaultSurrogate(NxSerializationSurrogate value)
    {
        this.mDefaultSurrogate = value;
    }
    
    /**
     * Use this to determine whether the specified surrogate is a built-in surrogate
     * @return true if the specified surrogate is a built-in surrogate
     */
    public boolean isBuiltinSurrogate(NxSerializationSurrogate surrogate)
    {
        if(surrogate == null)
            throw new IllegalArgumentException();
        return NxBuiltinSerializationSurrogate.class.isAssignableFrom(surrogate.getClass());
    }

    /**
     * Finds and returns an appropriate <tt>NxSerializationSurrogate</tt> for the given
     * Object. If no surrogate is found for the specified object, the <tt>Default"<tt/>
     * surrogate is returned.
     *
     * @param graph the Object whose surrogate is to be found.
     * @return an <tt>NxSerializationSurrogate</tt> Object
     * @see NxSerializationSurrogate
     */
    public NxSerializationSurrogate getSurrogateForObject(Object graph)
    {
        if (graph == null)
            return mNullSurrogate;
        return getSurrogateForType(graph.getClass());
    }

    /**
     * Finds and returns an appropriate <tt>NxSerializationSurrogate</tt> for the given
     * type. If no surrogate is found for the specified type, the <tt>Default"<tt/>
     * surrogate is returned.
     *
     * @param type the Class whose surrogate is to be found.
     * @return an <tt>NxSerializationSurrogate</tt> Object
     * @see NxSerializationSurrogate
     */
    public NxSerializationSurrogate getSurrogateForType(Class type)
    {
        NxSerializationSurrogate surrogate = (NxSerializationSurrogate) mTypeSurrogateMap.get(type);
        if (surrogate == null)
            surrogate = mDefaultSurrogate;
        return surrogate;
    }

    /**
     * Finds and returns an appropriate <tt>NxSerializationSurrogate</tt> for the given
     * Object. Allows you to inhibit the usage of default surrogate. If no surrogate is 
     * found for the specified object and strict is false, the <tt>Default"<tt/> surrogate 
     * is returned.
     *
     * @param type the Class whose surrogate is to be found.
     * @param strict specified whether the default surrogate will be returned in case of
     * no match.
     * @return an <tt>NxSerializationSurrogate</tt> Object if a match is found, otherwise 
     * retursn null or the default surrogate depending upon the value of "strict".
     * @see NxSerializationSurrogate
     */
    public NxSerializationSurrogate getSurrogateForType(Class type, boolean strict)
    {
        NxSerializationSurrogate surrogate = (NxSerializationSurrogate) mTypeSurrogateMap.get(type);
        if (!strict && surrogate == null)
            surrogate = mDefaultSurrogate;
        return surrogate;
    }

    /**
     * Finds and returns an appropriate <tt>NxSerializationSurrogate</tt> for the given
     * type handle. If no surrogate is found for the specified object, the <tt>Default"<tt/> surrogate 
     * is returned.
     *
     * @param handle the handle of the surrogate that is to be returned.
     * @return an <tt>NxSerializationSurrogate</tt> Object if a match is found, otherwise 
     * retursn the default surrogate.
     * @see NxSerializationSurrogate
     */
    public NxSerializationSurrogate getSurrogateForTypeHandle(short handle)
    {
        NxSerializationSurrogate surrogate = (NxSerializationSurrogate)mHandleSurrogateMap.get(handle);
        if (surrogate == null)
            surrogate = mDefaultSurrogate;
        return surrogate;
    }
    
    /**
     * Registers the specified <tt>NxSerializationSurrogate<tt/> with the system.
     *
     * @param surrogate specified surrogate
     * @return false if the surrogated type already has a surrogate
     */
    public boolean register(NxSerializationSurrogate surrogate)
         throws NxArgumentException
    {
        if (surrogate == null)
            throw new NxArgumentException();

        for (;;)
        {
            try {
                return register(surrogate, ++mNewTypeHandle);
            } catch (IllegalArgumentException e) { 
                continue;
            } catch (NxArgumentException e) {
                throw e;
            } catch (Exception e) {
                throw new NxArgumentException(e);
            }
        }
    }

    /**
     * Registers the specified <tt>NxSerializationSurrogate<tt/> with the given type handle.
     * Gives more control over the way type handles are generated by the system and allows the 
     * user to supply *HARD* handles for better interoperability among applications.
     *
     * @param surrogate specified surrogate
     * @param typeHandle specified HARD handle for type. <code>typeHandle<code/> must be in the 
     * range <code>NxTypeSurrogateSelector.MinTypeHandle<code/> and <code> NxTypeSurrogateSelector.MaxTypeHandle<code/> (inclusive)
     * @return false if the surrogated type already has a surrogate
     */
    public boolean register(NxSerializationSurrogate surrogate, short typeHandle)
     throws NxArgumentException
    {
        if (!isBuiltinSurrogate(surrogate))
        {
            if (typeHandle < NxTypeSurrogateConstants.MinTypeHandle)
            {
                throw new NxArgumentException(NxPackageResources.Surrogates_HandleOutOfRange);
            }
        }

        synchronized(mTypeSurrogateMap)
        {
            if (mHandleSurrogateMap.containsKey(typeHandle))
                throw new IllegalArgumentException(NxPackageResources.Surrogates_AlreadyRegistered);

            if (!mTypeSurrogateMap.containsKey(surrogate.getClassHandle()))
            {
                surrogate.setClassHandle(typeHandle);
                mTypeSurrogateMap.put(surrogate.getRealClass(), surrogate);
                mHandleSurrogateMap.put(surrogate.getClassHandle(), surrogate);
                return true;
            }
        }
        return false;
    }

    /**
     * Unregisters the specified <tt>NxSerializationSurrogate<tt/> from the system.
     *
     * @param surrogate specified surrogate
     */
    public void unregister(NxSerializationSurrogate surrogate)
    {
        if (surrogate == null)
            throw new IllegalArgumentException();

        synchronized(mTypeSurrogateMap)
        {
            mTypeSurrogateMap.remove(surrogate.getRealClass());
            mHandleSurrogateMap.remove(surrogate.getClassHandle());
        }
    }
        
    /** 
     * Unregisters all surrogates, except null and default ones.
     */
    public void clear() 
    {
        synchronized(mTypeSurrogateMap)
        {
            mTypeSurrogateMap.clear();
            mHandleSurrogateMap.clear();

            mNewTypeHandle = NxTypeSurrogateConstants.MinTypeHandle;
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The GNU General Public License (GPLv3)


Written By
Architect
Pakistan Pakistan
Let a = b ....... (1)
a - b = a - b
a^2 - ab = a^2 - ab
a^2 - ab = a^2 - b^2 (from 1)
a (a - b) = (a + b) (a - b)
a = (a + b) ...... (2)

if a = 1
1 = (1 + 1) (from 1 & 2)
1 = 2 !!

Comments and Discussions