Mecanismo de Busca ( Via atributos )

O sistema funciona como um mecanismo de busca. Nesta versão o sistema está apenas na classe AttributeManager
Segue um exemplo junto ao arquivo.

manager.Manager

package manager;

public class Manager
{
    public static void main(String[] args)
    {
        AttributeManager<Client> clients = new AttributeManager<Client>();
        clients.add(new Client("Joao Carlos da Nobrega", 25, true));
        clients.add(new Client("Pedro Silva Moreira", 17, true));
        clients.add(new Client("Barbara Almeida", 40, false));

        Client select = clients.get(Client.NAME, "Pedro Silva Moreira");
        System.out.println(select.getName() + " - Idade: " + select.getAge()); //Pedro Silva Moreira - Idade: 17

        select = clients.get(Client.AGE, 25);
        System.out.println(select.getName() + " - Idade: " + select.getAge()); //Joao Carlos da Nobrega - Idade: 25

        AttributeManager<Client> select2 = clients.getAll(Client.SEX, true);
        System.out.println("Foram selecionados " + select2.size() + " clientes com sexo masculino"); //Foram selecionados 2 clientes com sexo masculino
    }
}

manager.Client.java

[code]package manager;

public class Client extends AttributeManager<Object>
{
public static final int
NAME = 0,
AGE = 1,
SEX = 2; //True to male, false to female

public Client(String fname, int fage, boolean fsex)
{
    setName(fname);
    setAge(fage);
    setSex(fsex);
}

//Name
public String getName()
{
    return (String) get(NAME);
}

public final void setName(String fname)
{
    set(NAME, fname);
}

//Age
public int getAge()
{
    return (Integer) get(AGE);
}

public final void setAge(int fage)
{
    set(AGE, fage);
}

//Sex
public boolean getSex()
{
    return (Boolean) get(SEX);
}

public final void setSex(boolean fsex)
{
    set(SEX, fsex);
}

}
[/code]
[color=red]manager.AttributeManager.java[/color]

[code]package manager;

import java.util.ArrayList;

public class AttributeManager<T>
{
protected ArrayList<T> attributes = new ArrayList<T>();

private void debug(String fstring)
{
    System.out.println(&quot;AttributeManager: &quot; + fstring);
}

//Size
public int size()
{
    return attributes.size();
}

//Get
public T get(int ftype)
{
    if(size() &gt; ftype)
    {
        return attributes.get(ftype);
    }
    return null;
}

@SuppressWarnings(&quot;unchecked&quot;)
public T get(int ftype, Object fvalue)
{
    AttributeManager&lt;Object&gt; fin;
    
    for(int all = 0; all &lt; size(); ++all)
    {
        fin = (AttributeManager&lt;Object&gt;) get(all);
        if(fin != null && fin.get(ftype) != null && fin.get(ftype).equals(fvalue))
        {
            return (T) fin;
        }
    }
    return null;
}

@SuppressWarnings("unchecked")
public T get(int ftype, Object fvalue, int ftype2, Object fvalue2)
{
    AttributeManager&lt;Object&gt; fin;
    
    for(int all = 0; all &lt; size(); ++all)
    {
        fin = (AttributeManager&lt;Object&gt;) get(all);
        if(fin != null && fin.get(ftype) != null && fin.get(ftype).equals(fvalue) && fin.get(ftype2).equals(fvalue2))
        {
            return (T) fin;
        }
    }
    return null;
}

@SuppressWarnings("unchecked")
public AttributeManager&lt;T&gt; getAll(int ftype, Object fvalue)
{
    AttributeManager&lt;Object&gt; fin;
    AttributeManager&lt;T&gt; freturn = new AttributeManager&lt;T&gt;();
    
    for(int all = 0; all &lt; size(); ++all)
    {
        fin = (AttributeManager&lt;Object&gt;) get(all);
        if(fin != null && fin.get(ftype) != null && fin.get(ftype).equals(fvalue))
        {
            freturn.add((T) fin);
        }
    }
    
    return freturn;
}

@SuppressWarnings("unchecked")
public AttributeManager&lt;T&gt; getAll(int ftype, Object fvalue, int ftype2, Object fvalue2)
{
    AttributeManager&lt;Object&gt; fin;
    AttributeManager&lt;T&gt; freturn = new AttributeManager&lt;T&gt;();
    
    for(int all = 0; all &lt; size(); ++all)
    {
        fin = (AttributeManager&lt;Object&gt;) get(all);
        if(fin != null && fin.get(ftype) != null && fin.get(ftype).equals(fvalue) && fin.get(ftype2).equals(fvalue2))
        {
            freturn.add((T) fin);
        }
    }
    
    return freturn;
}

@SuppressWarnings({ &quot;rawtypes&quot;, &quot;unchecked&quot; })
public T get(int ftype, int finside, Object fvalue)
{
    AttributeManager ftmp;
    
    for(int all = 0; all &lt; size(); ++all)
    {
        ftmp = (AttributeManager) get(all);
        
        if(((AttributeManager) ftmp.get(ftype)).get(finside).equals(fvalue))
        {
            return (T) ftmp;
        }
    }
    return null;
}

//Extra get
public T first()
{
    return get(0);
}

public T back()
{
    if(size() &gt; 0)
        return attributes.remove(0);
    return null;
}

public T last()
{
    return get(size() - 1);
}

public T pop()
{
    if(size() &gt; 0)
    {
        T freturn = last();
        remove(size() - 1);
    
        return freturn;
    }
    return null;
}

//Set
public void add(T fvalue)
{
    attributes.add(fvalue);
}

public void add(int ftype, T fvalue)
{
    attributes.add(ftype, fvalue);
}

public void set(int ftype, T fvalue) //Set object value by type
{
    if(size() &gt; ftype)
    {
        attributes.set(ftype, fvalue);
    }
    else
    {
        for(int all = size(); all &lt; ftype; ++all)
            add(null);
        add(fvalue);
    }
}

@SuppressWarnings({ &quot;rawtypes&quot;, &quot;unchecked&quot; })
public void setAll(int ftype, Object fvalue)
{
	for(int fall = 0; fall &lt; size(); ++fall)
	{
		((AttributeManager) get(fall)).set(ftype, fvalue);
	}
}

//Remove
public void remove(int ftype) //Remove object by type
{
    if(size() &gt; ftype)
    {
        if(ftype == size() - 1)
        {
            attributes.remove(ftype);
            
            for(int all = ftype - 1; all &gt;= 0; --all)
            {
                if(get(all) == null)
                {
                    attributes.remove(all);
                }
                else break;
            }
        }
        else attributes.set(ftype, null);
    }
    else debug(&quot;Invalid position on remove.&quot;);
}

@SuppressWarnings(&quot;rawtypes&quot;)
public void remove(int ftype, Object fvalue)
{
    for(int all = 0; all &lt; size(); ++all)
    {
        if(((AttributeManager) get(all)).get(ftype).equals(fvalue))
        {
            attributes.remove(all);
            return;
        }
    }
    debug(&quot;Unale te remove.&quot;);
}

public void remove(Object fvalue)
{
    attributes.remove(fvalue);
}

public T removeFull(int ftype) //Remove without let null
{
    return attributes.remove(ftype);
}

public void clean()
{
    attributes.clear();
}

//Simplifications
public String concat()
{
    String fresult = &quot;&quot;;
    for(int all = 0; all &lt; attributes.size(); ++all)
    {
        if(fresult.length() &gt; 0)
            fresult = fresult + &quot; &quot;;
        fresult = fresult + get(all);
    }
    
    return fresult;
}

//Extra
public ArrayList&lt;T&gt; array()
{
    return this.attributes;
}

}[/code]

É isso, obrigado por ler e espero contribuir.
Atenciosamente, Pedro Silva Moreira - PeJuGe.

Muito bom parabéns :wink:

Bem interessante…

Valew pela contribuição !!

O tópico foi renomeado e o conteúdo é referente à versão 2.0 do Sistema de Atributos. Lembrando que o post era da versão 1.0