/**
* Speedo: an implementation of JDO compliant personality on top of JORM generic
* I/O sub-system.
* Copyright (C) 2001-2004 France Telecom R&D
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
*
*
* Contact: speedo@objectweb.org
*
* Authors: S.Chassande-Barrioz.
*
*/
package org.objectweb.speedo.metadata;
import java.io.Serializable;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.List;
import org.objectweb.speedo.api.SpeedoException;
import org.objectweb.speedo.api.SpeedoRuntimeException;
import org.objectweb.speedo.lib.Personality;
/**
* Description of all elements which can contain an extension.
* @author S.Chassande-Barrioz
*/
public abstract class SpeedoElement implements Serializable {
private static final long serialVersionUID = 457503247952030234L;
public Personality personality;
/**
* The extension corresponding to the element.
*/
public transient List jdoExtension = new ArrayList();
public SpeedoElement() {
SpeedoDefaults.setDefaults(this);
}
public String getExtensionValueByKey(String key) {
SpeedoExtension se = getExtensionByKey(key);
return (se == null ? null : se.value);
}
public SpeedoExtension getExtensionByKey(String key) {
if (jdoExtension == null || key == null) {
return null;
}
for(int i=0; i<jdoExtension.size(); i++) {
SpeedoExtension se = (SpeedoExtension) jdoExtension.get(i);
if (key.equals(se.key)) {
return se;
}
}
return null;
}
public SpeedoExtension getExtension(String vendor, String key) {
if (jdoExtension == null || vendor == null || key == null) {
return null;
}
for(int i=0; i<jdoExtension.size(); i++) {
SpeedoExtension se = (SpeedoExtension) jdoExtension.get(i);
if (vendor.equals(se.vendorName) && key.equals(se.key)) {
return se;
}
}
return null;
}
public void addExtension(SpeedoExtension se) {
jdoExtension.add(se);
}
protected static Object[] setValueInArray(Object value,
Object[] array,
int pos,
int arraySize) {
if (array == null) {
array = new Object[arraySize];
} else if (array.length <= pos) {
Object[] values = new Object[array.length + 1];
System.arraycopy(array, 0, values, 0, array.length);
array = values;
}
array[pos] = value;
return array;
}
protected static int indexOfInArray(Object[] array, Object element) {
int idx;
for (idx = 0; idx < array.length && array[idx] != element; idx++);
return idx;
}
protected static Object[] addInArray(Object value, Object[] array, Class type) {
Object[] newArray;
if (array == null) {
newArray = (Object[]) Array.newInstance(type.getComponentType(), 1);
newArray[0] = value;
} else {
newArray = (Object[]) Array.newInstance(type.getComponentType(), array.length + 1);
System.arraycopy(array, 0, newArray, 0, array.length);
newArray[array.length] = value;
}
for (int i = 0; i < newArray.length; i++) {
if (newArray[i] == null) {
throw new SpeedoRuntimeException("a null element found: " + i);
}
}
return newArray;
}
protected static Object[] removeInArray(Object value, Object[] array, Class type) {
Object[] newArray;
if (array == null || array.length == 0) {
return array;
} else {
newArray = (Object[]) Array.newInstance(type.getComponentType(), array.length - 1);
int idx = 0;
for (int i = 0; i < array.length; i++) {
if (array[i] != value) {
newArray[idx] = array[i];
}
idx ++;
}
}
for (int i = 0; i < newArray.length; i++) {
if (newArray[i] == null) {
throw new SpeedoRuntimeException("a null element found: " + i);
}
}
return newArray;
}
}