//================================================================================================== // D a t a _ s t r u c t u r e Java // E x t e n s i o n // E x t e n d a b l e O b j e c t // 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 an object that can carry additional properties. */
// Package //--------------------------------------------------------------------------------------- package bpp.data_structure.extension;
// Importation //----------------------------------------------------------------------------------- import java.util.Collection; import java.lang.Exception; import java.util.Iterator; import java.util.Vector;
@SuppressWarnings("unchecked")
// E x t e n d a b l e O b j e c t Class //-------------------------------------------------------- /*CLASS ExtendableObject */ /* Represents an <I>extendable</I> object, i.e. that can have extra attributes that are objects implementing the <CODE>Property</CODE> interface. */ public class ExtendableObject { //---------------------------------------------------------------------------------------Attributes protected Vector atPropertyS; // List of the additional properties. //--------------------------------------------------------------------------------------Constructor /*METHOD ExtendableObject */ /* Builds an extendable object. */ public ExtendableObject() { atPropertyS=new Vector(); } //---------------------------------------------------------------------------------------Properties /*METHOD ExtendableObject */ /* Returns a collection of the additional properties of the object. */ public Collection properties() { Object lcProperty;
Iterator lcIterator = atPropertyS.iterator(); Vector lcPropertyS = new Vector();
while (lcIterator.hasNext()) { lcProperty=lcIterator.next(); if (lcProperty!=null) lcPropertyS.add(lcProperty); }
return (lcPropertyS); } //-----------------------------------------------------------------------------------------Property /*METHOD ExtendableObject */ /* Returns the additional property associated with a given index. Usually, this method is called through a property map (that should be the only object to know the index of the property). */ public Property property(int agIndex) throws Exception { if (agIndex>=atPropertyS.size()) throw new Exception("Data Structure - Invalid property index."); return ((Property)atPropertyS.elementAt(agIndex)); } //--------------------------------------------------------------------------------------SetProperty /*METHOD ExtendableObject */ /* Sets the additional property associated with a given index. Usually, this method is called through a property map (that should be the only object to know the index of the property). */ public void setProperty(Property agProperty,int agIndex) { while (agIndex>atPropertyS.size()) atPropertyS.add(null);
if (agIndex==atPropertyS.size()) atPropertyS.add(agProperty); else atPropertyS.setElementAt(agProperty,agIndex); } //-----------------------------------------------------------------------------------------ToString /*METHOD ExtendableObject */ /* Returns a string that fully describes the state of the object. */ public String toString() { Iterator lcIterator = properties().iterator(); String lcString = "";
while (lcIterator.hasNext()) { lcString+=(Property)lcIterator.next(); if (lcIterator.hasNext()) lcString+=" , "; }
return (lcString); } //---------------------------------------------------------------------------------------FromString /*METHOD ExtendableObject */ /* Changes the state of the object according to the description provided by a given string. */ public void fromString(String agString) throws Exception { Property lcProperty;
Iterator lcIterator = properties().iterator(); String lcString = agString;
while (lcIterator.hasNext()) { lcProperty=(Property)lcIterator.next();
if (lcIterator.hasNext()) { lcProperty.fromString(lcString.substring(0,lcString.indexOf(" , "))); lcString=lcString.substring(lcString.indexOf(" , ")+3); } else lcProperty.fromString(lcString); } } }
// End //------------------------------------------------------------------------------------------- |
|