Wednesday, June 22, 2005

Problema Estructura

/*
* Created on 06/18/2005
*
* Manejo de estructuras y indices. Dado el ejemplo de una base de datos
*
* a) Los ordena segun el valor de uno de los campos elegidos
* b) Crear un indice para la busqueda a partir de un campo elegido
*/
package estructura;

import java.lang.reflect.Field;
import java.util.Comparator;
import java.util.LinkedList;
import java.util.Vector;

/**
* @author vns
*
*/
public class Estructura {

/**
*
*/
public Estructura() {
super();
}

public static void main(String[] args) {

}
}

class Persona {
private String pNombre;
private String pApellido;
private int edad;

/**
* @param nombre
* @param apellido
* @param edad
*/
public Persona(String nombre, String apellido, int edad) {
super();
pNombre = nombre;
pApellido = apellido;
this.edad = edad;
}

/**
*
*/
public Persona() {
}

/**
* @return Returns the pApellido.
*/
public String getPApellido() {
return pApellido;
}

/**
* @param apellido The pApellido to set.
*/
public void setPApellido(String apellido) {
pApellido = apellido;
}

/**
* @return Returns the pNombre.
*/
public String getPNombre() {
return pNombre;
}

/**
* @param nombre The pNombre to set.
*/
public void setPNombre(String nombre) {
pNombre = nombre;
}

/**
* @return Returns the edad.
*/
public int getEdad() {
return edad;
}

/**
* @param edad The edad to set.
*/
public void setEdad(int edad) {
this.edad = edad;
}

/*
* Display the persona
*/
public String print() {

return pNombre + " " + pApellido + " " + edad;
}

}

class HighArrayPersona
{
private Persona[] a;
private int nElems;
LinkedList index = new LinkedList();

public HighArrayPersona(int max)
{
a = new Persona[max];
nElems = 0;
}

public boolean find(String searchKey)
{
int j;
for(j=0; j < nElems; j++)
if(a[j].getPNombre().toString() == searchKey)
break;
if(j == nElems)
return false;
else
return true;
}

public void insert(Persona value)
{
a[nElems] = value;
nElems++;
}

public boolean delete(Persona value)
{
int j;
for(j=0; j < nElems; j++)
if( value == a[j] )
break;
if(j==nElems)
return false;
else
{
for(int k=j; k < nElems; k++)
a[k] = a[k+1];
nElems--;
return true;
}
}

public void display()
{
for(int j=0; j < nElems; j++)
a[j].print();
System.out.println("");
}
//----------------------------------------------------------
// sort elements
public void sort(String field){
Object sample = new Persona();
FieldComparator fieldComparator = new FieldComparator(sample, field);
int i;

for (i = 0; i < nElems; i++)
for (int j = 0; j < nElems; j++)
if ( fieldComparator.compare(a[i],a[j]) <= -1 )
swap(i,j);
}

// create the index for a field
public void createindex(String fields){
for(int i=0; i < nElems; i++)
if ( a[i] != null )
index.add( i, FieldComparator.getField(a[i],fields) ); // add all field of the objects
}

// changes the position i --> j
private void swap(int i, int j){
Persona temp = a[i];
a[i] = a[j];
a[j] = temp;
}

// delete duplicates
public void noDups()
{
int j;
for(j=0; j < nElems; j++) // look for it
for (int i = j; i < nElems; i++)
if( (a[j] == a[i]) && (i != j) && (a[j] != null) && (a[i] != null) )
a[i] = null;

if (j == nElems) // can't find it
return;

for(int k=0; k < nElems; k++){
if ( a[k] == null )
delete(a[k]); // delete all -1 value
}
} // end noDups()
//----------------------------------------------------------

} // end class HighArray

class FieldComparator implements Comparator{

Field[] publicField;
Vector fieldVec;
String SortBY;
boolean nameMatch = true;

/**
*
*/
public FieldComparator() {
super();
}


/**
* @param sample
* @param sortBY
*/
public FieldComparator(Object sample, String sortBY) {
SortBY = sortBY;
Class c = sample.getClass();
fieldVec = new Vector();
//TODO check for SecurityException don't apper
publicField = c.getFields();
for (int i = 0; i < publicField.length; i++) {
fieldVec.add( publicField[i].getName() );
}

nameMatch = fieldVec.contains(sortBY);
}// End constructor


/**
* @return Returns the sortBY.
*/
public String getSortBY() {
return SortBY;
}

/**
* @param sortBY The sortBY to set.
*/
public void setSortBY(String sortBY) {
SortBY = sortBY;
}

/**
* You should set the field to compared, set SortBY or in the constructor
*
* @param the objects to compared, arg0 and arg1
*
* @return the value 0 if the argument Object is equal to this Object;
* a value less than 0 if this Object is lexicographically less than the Object argument;
* and a value greater than 0 if this Object is lexicographically greater than the Object argument.
*/
public int compare(Object arg0, Object arg1) {

String str0 = getField(arg0, getSortBY());
String str1 = getField(arg1, getSortBY());

return str0.compareTo(str1);
}

static public String getField(Object obj, String field){
Class c = obj.getClass();
String string = "";

try {
// TODO check for SecurityException don't apper
Field field0 = c.getField( field );

string = (String)field0.get(obj);

} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (SecurityException e) {
e.printStackTrace();
} catch (NoSuchFieldException e) {
e.printStackTrace();
}
return string;
}
}

0 Comments:

Post a Comment

<< Home