//================================================================================================== // D a t a _ s t r u c t u r e Java // E x t e n s i o n // P r o p e r t y M a p // By Bruno Bachelet //================================================================================================== // Copyright (c) 1999-2016 // Bruno Bachelet - bruno@nawouak.net - http://www.nawouak.net // // This file is part of the B++ Library. This library is free software; you can redistribute it // and/or modify it under the terms of the GNU Library General Public License as published by the // Free Software Foundation; either version 2 of the License, or (at your option) any later // version. // // This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; // without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See // the GNU Library General Public License for more details (http://www.gnu.org).
/*DESCRIPTION*/ /* This module provides a class to model a so-called <I>property map</I> that allows to access a given additional property for a set of extendable objects. */
// Package //--------------------------------------------------------------------------------------- package bpp.data_structure.extension;
// Importation //----------------------------------------------------------------------------------- import java.util.StringTokenizer;
// P r o p e r t y M a p Class //------------------------------------------------------------------ /*CLASS PropertyMap */ /* Represents an additional property for a set of extendable objects and the way to access this property for each one of them. It is called a <I>property map</I> and is intended to be used with the abstract class <CODE>ExtendableContainer</CODE>. */ public class PropertyMap { //---------------------------------------------------------------------------------------Attributes protected int atIndex; // Index of the property in the extendable objects' list. protected String atName; // Name of the property. protected Property atTemplate; // Default value of the property. //--------------------------------------------------------------------------------------Constructor /*METHOD PropertyMap */ /* Builds a null property map. */ public PropertyMap() { atIndex=0; atName="unknown"; atTemplate=null; } //--------------------------------------------------------------------------------------Constructor /*METHOD PropertyMap */ /* Builds a property map with given name, index and template property. The name is a string that identifies uniquely an additional property for a set of extendable objects. The index is the position of the additional property in the extendable objects' list. The template property is the default value of the additional property when it is first attached to an extendable object. */ public PropertyMap(String agName,int agIndex,Property agTemplate) { atIndex=agIndex; atName=agName; atTemplate=agTemplate; } //--------------------------------------------------------------------------------------------Index /*METHOD PropertyMap */ /* Returns the index of the property in the extendable objects' list. */ public int index() { return (atIndex); } //---------------------------------------------------------------------------------------------Name /*METHOD PropertyMap */ /* Returns the name of the property. */ public String name() { return (atName); } //-----------------------------------------------------------------------------------------Template /*METHOD PropertyMap */ /* Returns the default value of the property. */ public Property template() { return (atTemplate); } //----------------------------------------------------------------------------------------------Get /*METHOD PropertyMap */ /* Returns the additional property of a given extendable object. */ public Property get(ExtendableObject agExtendable) throws Exception { return (agExtendable.property(atIndex)); } //----------------------------------------------------------------------------------------------Set /*METHOD PropertyMap */ /* Sets the additional property of a given extendable object. */ public void set(ExtendableObject agExtendable,Property agProperty) { agExtendable.setProperty(agProperty,atIndex); } //-----------------------------------------------------------------------------------------ToString /*METHOD PropertyMap */ /* Returns a string that fully describes the state of the property map. */ public String toString() { String lcString = "property "+atIndex+" = "+atName+" , ";
if (atTemplate==null) lcString+="nil ; nil"; else lcString+=atTemplate.getClass().getName()+" ; "+atTemplate.toString();
return (lcString); } //---------------------------------------------------------------------------------------FromString /*METHOD PropertyMap */ /* Changes the state of the property map according to the description provided by a given string. */ public void fromString(String agString) throws Exception { Class lcClass; int lcIndex; String lcName; String lcString; Property lcTemplate;
StringTokenizer lcTokenizer = new StringTokenizer(agString);
lcString=lcTokenizer.nextToken();
if (!lcString.equals("property")) throw new Exception ("Data Structure - Can't extract property map information from string.");
lcIndex=Integer.parseInt(lcTokenizer.nextToken()); lcTokenizer.nextToken(); lcName=lcTokenizer.nextToken(); lcTokenizer.nextToken(); lcClass=Class.forName(lcTokenizer.nextToken()); lcTemplate=(Property)lcClass.newInstance(); lcTemplate.fromString(agString.substring(agString.lastIndexOf(" ; ")+3));
atIndex=lcIndex; atName=lcName; atTemplate=lcTemplate; } }
// End //------------------------------------------------------------------------------------------- |
|