Shallow persistence

Discussion in 'Java' started by Carlos Sari, Oct 27, 2003.

  1. Carlos Sari

    Carlos Sari Guest

    I need to save the current state of an object, without it´s
    composition objects (deep persistence), just it´s reference, id, or a
    kind of proxy. Is there a way to use shallow persistence instead?
     
    Carlos Sari, Oct 27, 2003
    #1
    1. Advertising

  2. Carlos Sari wrote:

    > I need to save the current state of an object, without it´s
    > composition objects (deep persistence), just it´s reference, id, or a
    > kind of proxy. Is there a way to use shallow persistence instead?


    More context is needed to answer the question. If you mean to do this
    via Java serialization then the information you need is in the API docs
    for java.io.Serializable.

    Otherwise, we need more information. How do you expect this ability to
    be used? Do you recognize that retrieving an object from any kind of
    storage external to the JVM means making / getting a copy of the
    original object, not the original object itself? (This is
    distinguishable if you still have the original.) How will the recovered
    object reconstitute the rest of its state? (Or does it need to do?) It
    may be possible to write code for this, but you need to define the
    requirements in much more detail.


    John Bollinger
     
    John C. Bollinger, Oct 27, 2003
    #2
    1. Advertising

  3. Carlos Sari

    Roedy Green Guest

    On 27 Oct 2003 08:18:15 -0800, (Carlos Sari) wrote
    or quoted :

    >I need to save the current state of an object, without it´s
    >composition objects (deep persistence), just it´s reference, id, or a
    >kind of proxy. Is there a way to use shallow persistence instead?


    the tools you have are the Preferences class to store key=value
    strings in the registry. You can store serialised objects. You can
    store binary values in ordinary files. You can store text files.

    For reading key=value,value properties files, you might find this
    class useful.

    package com.mindprod.replicator;

    import java.io.BufferedReader;
    import java.io.FileInputStream;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.InputStreamReader;
    import java.util.Enumeration;
    import java.util.Hashtable;
    import java.util.regex.Pattern;

    /**
    * This class is similar to java.util.Properties.
    * The properties file has a similar key=value format.
    * However, for MultiProperties,
    * spaces in the values are not coded
    * as "\ ", just as plain " ".
    * Values can be a tuple separated by commas.
    * The inherited Hashtable get returns a String[].
    * There is no support for extended chars such as \n.
    * There is no support for \ as a continuation character.
    * Comment lines begin with #.
    * Blank lines are ignored.
    * The file must end with a line separator.
    * All values are trimmed of leading and trailing spaces.
    * All Strings are interned, so they behave just like
    * hard-coded String static finals.
    *
    * It is basically just a Hashtable with a load method
    * and a few conveniences added to the get method.
    *
    * @author Roedy Green
    * @version 1.0
    * @since 2003-08-03
    */
    public class MultiProperties extends Hashtable
    {

    /**
    * true if you want the debugging harness
    */
    private static final boolean DEBUG=false;

    /**
    * Constructs a new, empty hashtable with
    * the specified initial
    * capacity and the specified load factor.
    * See http://mindprod.com/jgloss/hashtable.html
    * for a full description of
    * what the initialCapacity and loadFactors mean.
    *
    * @param initialCapacity the initial capacity of the
    hashtable.
    * @param loadFactor the load factor of the hashtable.
    * @exception IllegalArgumentException if the initial capacity is
    less
    * than zero, or if the load factor is nonpositive.
    */
    public MultiProperties ( int initialCapacity, float loadFactor )
    {
    super( initialCapacity, loadFactor );
    }

    /**
    * Load the properties hashtable
    * from a text file of key=value pairs.
    *
    * @param in where to load the textual key=value pairs from.
    * @exception IOException
    */
    public void load ( InputStream in ) throws IOException
    {
    BufferedReader br = new BufferedReader( new InputStreamReader(
    in ) );

    while ( true )
    {
    String line = br.readLine();
    if ( line == null )
    {
    /* eof */
    break;
    }
    if ( line.startsWith( "#" ) )
    {
    /* ignore comments */
    continue;
    }
    line = line.trim();
    if ( line.length() == 0 )
    {
    /* ignore blank lines */
    continue;
    }

    // split line into key and value
    String[] keyValue = keyValueSplitter.split( line );
    switch ( keyValue.length )
    {
    case 1:
    {
    // key=nothing
    String key = keyValue[0].trim().intern();
    if ( key.length() == 0 )
    {
    complain( line );
    }
    this.put( key, dummy );
    }
    break;

    case 2:
    {
    // key=value
    String key = keyValue[0].trim().intern();
    if ( key.length() == 0 )
    {
    complain( line );
    }
    // Split value into subfields
    String[] values = subFieldSplitter.split(
    keyValue[1].trim() );

    // trim the multiple values
    for ( int i=0; i<values.length; i++ )
    {
    values = values.trim().intern();
    }
    // save in the underlying Hashtable.
    this.put ( key, values );
    }
    break;

    default:
    case 0:
    complain( line );
    }
    } // end while
    br.close();
    } // end load

    /**
    * Complain about malformed data.
    *
    * @param line Line of key=value that has a problem.
    */
    private static void complain ( String line )
    {
    throw new IllegalArgumentException( "MultiProperties: malformed
    key=value : "
    + line );
    }

    /**
    * Get values associated with key.
    *
    * @param key Key, case sensitive.
    *
    * @return array of associated Strings, possibly dimension 0.
    * If key is undefined returns empty array, not null.
    */
    public String[] getMultiple ( String key )
    {
    String[] value = (String[]) get( key );
    if ( value == null )
    {
    return dummy;
    }
    else
    {
    return value;
    }
    }

    /**
    * Get value associated with key.
    *
    * @param key Key, case sensitive.
    * @param defaultValue
    * Value for the default if the key is not defined.
    * key=nothing returns "", not the default value.
    * @return String for a single value, or the first of a set of
    multiple values, or "".
    */
    public String get ( String key, String defaultValue )
    {
    Object value = get( key );
    if ( value == null )
    {
    return defaultValue.intern();
    }
    else
    {
    String[] array = ((String[])value);
    if ( array.length != 0 )
    {
    return array[0];
    }
    else
    {
    return ""; // not defaultValue!
    }
    }
    }

    /**
    * Get single integer value associated with key.
    *
    * @param key Key, case sensitive.
    * @param defaultValue
    * Value for the default if the key is not defined.
    * @return integer value of the key, or defaultValue if not defined
    or if key=
    * @exception NumberFormatException
    * if the value is not a valid integer.
    */
    public int getInt ( String key, int defaultValue ) throws
    NumberFormatException
    {
    Object value = get( key );
    if ( value == null )
    {
    return defaultValue;
    }
    else
    {
    String[] array = ((String[])value);
    if ( array.length != 0 )
    {
    return Integer.parseInt ( array[0] );
    }
    else
    {
    return defaultValue;
    }
    }

    }

    /**
    * Get boolean value associated with key.
    * Valid values for key are true, false, yes, no, case insensitive.
    *
    * @param key Key, case sensitive.
    * @param defaultValue
    * Value for the default if the key is not defined.
    * @return boolean value of the key, or defaultValue if not defined
    or if key=
    */
    public boolean getBoolean ( String key, boolean defaultValue )
    {
    Object value = get( key );
    if ( value == null )
    {
    return defaultValue;
    }
    else
    {
    String[] array = ((String[])value);
    if ( array.length != 0 )
    {
    return array[0].equalsIgnoreCase( "true" ) ||
    array[0].equalsIgnoreCase( "yes" );
    }
    else
    {
    return defaultValue;
    }
    }

    }

    /**
    * A dummy empty array of Strings.
    * No point is allocating a fresh one every time it is needed.
    */
    private static String[] dummy = new String[ 0 ];

    // Pattern to split line into key and value at the =
    private static Pattern keyValueSplitter = Pattern.compile ( "=" );

    /**
    * Pattern to split into words separated by commas.
    * Two commas in a row in the String to be matched gives an empty
    field
    */
    private static Pattern subFieldSplitter = Pattern.compile ( "," );

    /**
    * test harness
    *
    * @param args not used
    */
    public static void main ( String[] args )
    {
    if ( DEBUG )
    {
    MultiProperties m = new MultiProperties ( 100, .75f );
    try
    {
    m.load( new FileInputStream ( "replicator.properties" ) );
    }
    catch ( IOException e )
    {
    Persist.fatal( "Unable to read replicator.properties
    file."
    + "\n"
    + e.getMessage() );
    }
    for ( Enumeration e = m.keys(); e.hasMoreElements(); )
    {
    String key = (String) e.nextElement();
    String[] values = m.getMultiple( key );
    System.out.println( key + " =");
    for ( int i=0; i<values.length; i++ )
    {
    System.out.println ( values );
    }
    } // end for
    } // end if DEBUG
    } // end main
    } // end MultiProperties


    --
    Canadian Mind Products, Roedy Green.
    Coaching, problem solving, economical contract programming.
    See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
     
    Roedy Green, Oct 27, 2003
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. VisionSet
    Replies:
    8
    Views:
    4,963
    Tris Orendorff
    Apr 29, 2004
  2. javaguy44
    Replies:
    10
    Views:
    1,010
    Michael Berg
    May 18, 2004
  3. Gary Newell
    Replies:
    2
    Views:
    6,531
    Tilman Bohn
    Dec 13, 2004
  4. Tony Johansson

    deep and shallow copy

    Tony Johansson, May 19, 2005, in forum: C++
    Replies:
    5
    Views:
    9,870
    Rapscallion
    May 19, 2005
  5. Kenneth P. Turvey

    Java Persistence API and persistence.xml

    Kenneth P. Turvey, Mar 15, 2008, in forum: Java
    Replies:
    2
    Views:
    17,506
    Kenneth P. Turvey
    Mar 16, 2008
Loading...

Share This Page