⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 enhproperties.java

📁 Java的面向对象数据库系统的源代码
💻 JAVA
字号:
// You can redistribute this software and/or modify it under the terms of
// the Ozone Library License version 1 published by ozone-db.org.
//
// The original code and portions created by SMB are
// Copyright (C) 1997-@year@ by SMB GmbH. All rights reserved.
//
// $Id$


package org.ozoneDB.util;


import org.ozoneDB.DxLib.*;
import org.ozoneDB.io.stream.ResolvingObjectInputStream;

import java.io.*;
import java.util.*;

/**
 * EnhProperties has methods to store/update the value of a property to handle
 * such dynamic properties.
 * <p>
 * In addition the Properties EnhProperties can hold not only String properties
 * but most of other primitive types and raw objects. Non-string properties are
 * internaly stored as Strings.
 * <p>
 * Setup extends java.util.Properties. So the system properties can be used as
 * defaults.
 *
 *
 * @author <a href="http://www.softwarebuero.de/">SMB</a>
 * @version $Revision$Date$
 */
public class EnhProperties extends Properties {

    /**
     * An Observable object that is responsible for this properties. Directly
     * extending the class is not possible because it already extends Properties.
     */
    protected EnhObservable observable;

    public EnhProperties() {
        observable = new EnhObservable();
    }

    public EnhProperties( Properties _defaults ) {
        super( _defaults );
        observable = new EnhObservable();
    }

    public void addObserver( Observer _observer ) {
        observable.addObserver( _observer );
    }

    public void removeObserver( Observer _observer ) {
        observable.deleteObserver( _observer );
    }

    public void notifyObservers() {
        observable.notifyObservers( this );
    }

    public boolean hasChanged() {
        return observable.hasChanged();
    }

    /**
     * @param properties
     * @param keyPrefix
     */
    public void addProperties( Properties properties, String keyPrefix ) {
        observable.setChanged();
        for (Enumeration e = properties.keys(); e.hasMoreElements();) {
            String key = (String)e.nextElement();
            if (key.startsWith( keyPrefix )) {
                // System.out.println (key);
                String val = properties.getProperty( key, "" );
                setStringProperty( key, val );
            }
        }
    }


    /**
     * @param _val
     * @param _key
     */
    public synchronized void setStringProperty( String _key, String _val ) {
        observable.setChanged();
        super.put( _key, _val );
    }


    /**
     * @param _key
     * @param _default The default value to use if no property is found.
     */
    public String stringProperty( String _key, String _default ) {
        return super.getProperty( _key, _default );
    }


    /**
     * @param _key
     * @param _default The default value to use if no property is found.
     */
    public DxCollection stringsProperty( String _key, String _default ) {
        String propList = stringProperty( _key, _default );
        DxArrayBag result = new DxArrayBag();

        StringTokenizer st = new StringTokenizer( propList, " \t,", false );
        while (st.hasMoreTokens()) {
            String token = st.nextToken();
            // System.out.println ("'" + token + "'");
            result.add( token );
        }
        return result;
    }

    /**
     * @param _val
     * @param _key
     */
    public void setProperty( String _key, Object _val ) {
        observable.setChanged();
        try {
            ByteArrayOutputStream buf = new ByteArrayOutputStream( 1024 );
            ObjectOutputStream out = new ObjectOutputStream( buf );
            out.writeObject( _val );
            out.close();

            MimeBase64Encoder encoder = new MimeBase64Encoder();
            encoder.translate( buf.toByteArray() );

            setStringProperty( _key, new String( encoder.getCharArray() ) );
        } catch (Exception e) {
            throw new RuntimeException( e.getMessage() );
        }
    }


    /**
     * @param _key
     * @param _default The default value to use if no property is found.
     */
    public Object property( String _key, Object _default ) {
        try {
            String result = stringProperty( _key, (String)_default );
            if (result != null) {
                MimeBase64Decoder decoder = new MimeBase64Decoder();
                decoder.translate( result.toCharArray() );

                ObjectInputStream in = new ResolvingObjectInputStream( new ByteArrayInputStream( decoder.getByteArray() ) );
                return in.readObject();
            } else {
                return null;
            }
        } catch (Exception e) {
            throw new RuntimeException( e.getMessage() );
        }
    }


    /**
     * @param _val
     * @param _key
     */
    public void setIntProperty( String _key, int _val ) {
        setStringProperty( _key, String.valueOf( _val ) );
    }


    /**
     * @param _key
     * @param _default The default value to use if no property is found.
     */
    public int intProperty( String _key, int _default ) {
        String result = stringProperty( _key, String.valueOf( _default ) );
        return Integer.parseInt( result );
    }


    /**
     * @param _key
     * @param _default The default value to use if no property is found.
     */
    public long longProperty( String _key, long _default ) {
        String result = stringProperty( _key, String.valueOf( _default ) );
        return Long.valueOf( result ).longValue();
    }


    /**
     * @param _key
     * @param _default The default value to use if no property is found.
     */
    public boolean booleanProperty( String _key, boolean _default ) {
        String result = stringProperty( _key, String.valueOf( _default ) );
        return Boolean.valueOf( result ).booleanValue();
    }


    /**
     * @param _val
     * @param _key
     */
    public void setLongProperty( String _key, long _val ) {
        setStringProperty( _key, String.valueOf( _val ) );
    }

    /**
     * @param _val
     * @param _key
     */
    public void setBooleanProperty( String _key, boolean _val ) {

        setStringProperty( _key, String.valueOf( _val ) );

    }





    /**

     * @param _val

     * @param _key

     */

    public void setIntArrayProperty( String _key, int[] _val ) {
        StringBuffer strVal = new StringBuffer();
        for (int i = 0; i < _val.length; i++) {
            strVal.append( "," + _val[i] );
        }
        setStringProperty( _key, strVal.substring(1) );
    }





    /**

     * @param _key

     * @param _default The default value to use if no property is found.

     */

    public int[] intArrayProperty( String _key, int[] _default ) {
        String strVal = super.getProperty( _key );
        if (strVal == null) {
            return _default;
        } else {
            StringTokenizer st = new StringTokenizer( strVal, "," );
            int[] result = new int[st.countTokens()];
            for (int i = 0; i < result.length; i++) {
                result[i] = Integer.parseInt( st.nextToken() );
            }
            return result;
        }
    }





    /**

     * @param out

     * @param keyPrefix

     * @param printPrefix

     */

    public void print( PrintStream out, String keyPrefix, String printPrefix ) {

        DxTreeSet sortedProps = new DxTreeSet( new DxStringComparator() );

        for (Enumeration e = propertyNames(); e.hasMoreElements();) {

            String key = (String)e.nextElement();

            String val = stringProperty( key, "(not set)" );

            //avoid printing "object" properties

            //currently not supported!!!

            if (key.startsWith( keyPrefix ) && !val.startsWith( "[object]" )) {

                sortedProps.add( printPrefix + key + " = " + val );

            }

        }

        for (DxIterator it = sortedProps.iterator(); it.next() != null;) {

            out.println( it.object().toString() );

        }

    }



}





/**

 * @author <a href="http://www.softwarebuero.de/">SMB</a>

 * @version $Revision$Date$

 */

class EnhObservable extends Observable {





    public EnhObservable() {

        super();

    }





    public void setChanged() {

        super.setChanged();

    }



}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -