純日本製スクリプト言語 ruby を使い倒す。


キーストン ホーム

前に戻る
オブジェクトデータベース登録用にコンポジットパターンを実装してみた。

クラス宣言部分のみを抽出すると、とりあえず以下の4つがメインの実装。

public interface KsIbasic
public abstract class KsAbBasic implements com.keystonebrand.squid.lib.KsIbasic
public abstract class  KsAbComposite<T> extends KsAbBasic
public class KsName extends KsAbComposite<String>

使用例:
KsName wEgi = new KsName("超シャープな動きを実現するエギ");
KsName wEgiSharp = new KsName("エギシャープ");
KsName wMonroEgi = new KsName("モンローエギ");
KsName wMetalicall = new KsName("メタリカルカラー");

wEgi.add_child(wEgiSharp); 
wEgi.add_child(wMonroEgi);
wEgiSharp.add_child(wMetalicall);



/*
 * プロジェクト: com.keystonebrand.squid
 *
 * ファイル名: KsIbasic.java
 *
 * データベースへ登録時に最もベースとなるインターフェース
 *
 * author : Keystone kanagawa
 */

package com.keystonebrand.squid.lib;


public interface KsIbasic {
    public void setParent(com.keystonebrand.squid.domain.KsAbBasic aObject);
}



/*
 * プロジェクト: com.keystonebrand.squid
 *
 * ファイル名: KsAbBasic.java
 *
 * データベースへ登録時にベースとなる抽象クラス
 *
 * author : Keystone kanagawa
 */

package com.keystonebrand.squid.domain;

import java.util.Date;

/**
 *
 * @author nobu
 */
public abstract class KsAbBasic implements com.keystonebrand.squid.lib.KsIbasic {
    private java.util.Date ntime = null;                     //登録日時
    private java.util.Date mtime = null;                     //更新日時
    protected KsAbBasic parent = null;                       //親オブジェクト
    private com.keystonebrand.squid.domain.KsACL acl = null; //アクセス制御

    public KsAbBasic()
    {
        this.ntime = new java.util.Date();
        this.mtime = this.ntime;
        this.parent = null;
        this.acl = null;
    }

    public <T> void exec(com.keystonebrand.squid.lib.KsIblockBack<T> aBlockBack) {
        try
        {
            aBlockBack.exec((T)this);
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }
    }
    public Date getMtime() {
        return mtime;
    }

    public void setMtime(Date mtime) {
        this.mtime = mtime;
    }

    public Date getNtime() {
        return ntime;
    }

    public void setNtime(Date ntime) {
        this.ntime = ntime;
    }

    public KsAbBasic getParent() {
        return parent;
    }

    public void setParent(KsAbBasic parent) {
        this.parent = parent;
    }
    public void up() {
        this.mtime = new java.util.Date();
    }

    public KsACL getAcl() {
        return acl;
    }

    public void setAcl(KsACL acl) {
        this.acl = acl;
    }


    public void regist(com.keystonebrand.squid.model.KsIdbAccess aDbAccess) {
        try
        {
            if (aDbAccess == null)
            {
                throw new Exception("引数がおかしかばい");
            }
            this.up();
            aDbAccess.regist(this);
        }
        catch (Exception e)
        {
            com.keystonebrand.squid.property.KsInformation.getLogger().warning(e.getMessage());
        }
    }
    public static <Treturn, Tvalue> Treturn Create(String aClassName,
                                                    String aVarName,
                                                    Tvalue aValue,
                                                    KsAbComposite<Treturn> aParentNode,
                                                    com.keystonebrand.squid.model.KsIdbAccess aDbAccess,
                                                    boolean isExistCheck,
                                                    boolean isRegist) {
        Treturn wResult = null;
        try
        {
            if (aDbAccess == null)
            {
                throw new Exception("引数がおかしかばい");
            }
            if (isExistCheck)
            {
                wResult = (Treturn)aDbAccess.find_first(aClassName, aVarName, aValue);
            }

            if (wResult == null)
            {
                Class wClass = Class.forName(aClassName);
                wResult = (Treturn)wClass.getConstructor(aValue.getClass()).newInstance(aValue);
                if (aParentNode != null)
                {
                    aParentNode.add_child((KsAbComposite<Treturn>)wResult);
                }
            }

            if (isRegist)
            {
                aDbAccess.regist(wResult);
            }
        }
        catch (Exception e)
        {
            com.keystonebrand.squid.property.KsInformation.getLogger().warning(e.getMessage());
        }
        return wResult;
    }
}


/*
 * プロジェクト: com.keystonebrand.squid
 *
 * ファイル名: KsAbComposite.java
 *
 * コンポジットパターンの実装クラス
 *
 * author : Keystone kanagawa
 */

package com.keystonebrand.squid.domain;

import java.util.Vector;


public abstract class  KsAbComposite<T> extends KsAbBasic
{
    private T value = null;
    private Vector<KsAbComposite<T>> childs = null;

    public KsAbComposite(T aValue)
    {
        super();
        try
        {
            this.value = aValue;
            this.childs = null;
        }
        catch(Exception e)
        {
            com.keystonebrand.squid.property.KsInformation.getLogger().warning(e.getMessage());
        }
    }
    public void add_child(KsAbComposite<T> aNode)
    {
        try
        {
            if (this.childs == null)
            {
                this.childs = new Vector<KsAbComposite<T>>();
            }
            this.childs.add(aNode);
            aNode.setParent(this);
        }
        catch (Exception e)
        {
            com.keystonebrand.squid.property.KsInformation.getLogger().warning(e.getMessage());
        }
    }

    /**
        def recursive(aDepth=0, &block)
            yield self, aDepth
            if @childs
                aDepth+=1
                @childs.each do |iChild|
                    iChild.recursive aDepth, &block
                 end
             end
        end
     *
     */
    public void recursive(int aDepth, com.keystonebrand.squid.lib.KsIcallBackComposite<KsAbComposite<T>> aCallBack)
    {
        try
        {
            aCallBack.exec(aDepth, this);
            if (this.childs != null && this.childs.size() > 0)
            {
                aDepth++;
                for (KsAbComposite<T> wNode : this.childs)
                {
                    wNode.recursive(aDepth, aCallBack);
                }
            }
        }
        catch(Exception e)
        {
            com.keystonebrand.squid.property.KsInformation.getLogger().warning(e.getMessage());
        }
    }

    public Vector<KsAbComposite<T>> getChilds() {
        return childs;
    }

    public void setChilds(Vector<KsAbComposite<T>> childs) {
        this.childs = childs;
    }

    public T getValue() {
        return value;
    }

    public void setValue(T value) {
        this.value = value;
    }


    public <T2> void find_by_value(T aValue, com.keystonebrand.squid.model.KsIdbAccess aDbAccess,
                                    final com.keystonebrand.squid.lib.KsIcallBackBasic<T2> aCallBack) {
        try
        {
            if (aDbAccess == null)
            {
                throw new Exception("引数がおかしかばい");
            }

            aDbAccess.find_each(this.getClass().getName(), "value", aValue, new com.keystonebrand.squid.lib.KsIcallBackBasic<T>()
            {
                public void exec(T aObject) {
                    aCallBack.exec((T2)aObject);
                }
            });
        }
        catch (Exception e)
        {
            com.keystonebrand.squid.property.KsInformation.getLogger().warning(e.getMessage());
        }
    }
    public static <T1, T2> void FindByValue(Class aClass, T1 aValue,
                                              com.keystonebrand.squid.model.KsIdbAccess aDbAccess,
                                              final com.keystonebrand.squid.lib.KsIcallBackBasic<T2> aCallBack) {
        try
        {
            if (aDbAccess == null)
            {
                throw new Exception("引数がおかしかばい");
            }
            aDbAccess.find_each(aClass.getName(), "value", aValue, new com.keystonebrand.squid.lib.KsIcallBackBasic<T1>()
            {
                public void exec(T1 aObject) {
                    aCallBack.exec((T2)aObject);
                }
            });
        }
        catch (Exception e)
        {
            com.keystonebrand.squid.property.KsInformation.getLogger().warning(e.getMessage());
        }
    }

    public static <T, TgetValue> void Each(String aClassName, TgetValue aValue,
                                            com.keystonebrand.squid.model.KsIdbAccess aDbAccess,
                                            final com.keystonebrand.squid.lib.KsIcallBackBasic<T> aCallBack) {
        try
        {
            if (aDbAccess == null)
            {
                throw new Exception("引数がおかしかばい");
            }

            aDbAccess.find_each(aClassName, "value", aValue, new com.keystonebrand.squid.lib.KsIcallBackBasic<T>()
            {
                public void exec(T aObject) {
                    aCallBack.exec(aObject);
                }
            });
        }
        catch (Exception e)
        {
            com.keystonebrand.squid.property.KsInformation.getLogger().warning(e.getMessage());
        }
    }
    public static <Treturn, TgetValue> Treturn First(String aClassName,
                                                      TgetValue aValue,
                                                      com.keystonebrand.squid.model.KsIdbAccess aDbAccess) {
        Treturn wReturn = null;
        try
        {
            if (aDbAccess == null)
            {
                throw new Exception("引数がおかしかばい");
            }

            wReturn = (Treturn)aDbAccess.find_first(aClassName, "value", aValue);
        }
        catch (Exception e)
        {
            com.keystonebrand.squid.property.KsInformation.getLogger().warning(e.getMessage());
        }

        return wReturn;
    }

    public <TgetValue> com.keystonebrand.squid.domain.KsAbComposite<T> find_in_childs(TgetValue aValue) {
        com.keystonebrand.squid.domain.KsAbComposite<T> wReturn = null;

        try
        {
            for (com.keystonebrand.squid.domain.KsAbComposite<T> wObject : this.childs)
            {
                if (aValue.equals(wObject.getValue()))
                {
                    wReturn = wObject;
                    break;
                }
            }
        }
        catch (Exception e)
        {
            com.keystonebrand.squid.property.KsInformation.getLogger().warning(e.getMessage());
        }

        return wReturn;
    }
}



/*
 * プロジェクト:com.keystonebrand.squid
 *
 * データベースへ登録用クラス/Compositeパターンを継承するクラス。
 * Stringベースのオブジェクトに使う。
 *
 * 使用例:
 * KsName wEgi = new KsName("超シャープな動きを実現するエギ");
 * KsName wEgiSharp = new KsName("エギシャープ");
 * KsName wMonroEgi = new KsName("モンローエギ");
 * KsName wMetalicall = new KsName("メタリカルカラー");
 *
 * wEgi.add_child(wEgiSharp);
 * wEgi.add_child(wMonroEgi);
 * wEgiSharp.add_child(wMetalicall);
 *
 * author : Keystone kanagawa
 */


package com.keystonebrand.squid.domain;


public class KsName extends KsAbComposite<String> {
    private KsName memo = null;

    public KsName(String aValue)
    {
        super(aValue);
    }

    public KsName getMemo() {
        return memo;
    }

    public void setMemo(KsName memo) {
        this.memo = memo;
    }

    public static String Concat(KsName[] aNames, String aSeparator)
    {
        String wReturn = null;

        for (int wIndex = 0; wIndex < aNames.length; wIndex++)
        {
            if (wIndex == 0)
            {
                wReturn = aNames[wIndex].getValue();
            }
            else
            {
                wReturn = wReturn + aSeparator + aNames[wIndex].getValue();
            }
        }

        return wReturn;
    }
}

前に戻る