Mecanismo de Busca ( Via atributos )

3 respostas
PeJuGe

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
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);
    }
}
[color=red]manager.AttributeManager.java[/color]
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("AttributeManager: " + fstring);
    }
    
    //Size
    public int size()
    {
        return attributes.size();
    }
    
    //Get
    public T get(int ftype)
    {
        if(size() > ftype)
        {
            return attributes.get(ftype);
        }
        return null;
    }
    
    @SuppressWarnings("unchecked")
    public T get(int ftype, Object fvalue)
    {
        AttributeManager<Object> fin;
        
        for(int all = 0; all < size(); ++all)
        {
            fin = (AttributeManager<Object>) 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<Object> fin;
        
        for(int all = 0; all < size(); ++all)
        {
            fin = (AttributeManager<Object>) 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<T> getAll(int ftype, Object fvalue)
    {
        AttributeManager<Object> fin;
        AttributeManager<T> freturn = new AttributeManager<T>();
        
        for(int all = 0; all < size(); ++all)
        {
            fin = (AttributeManager<Object>) get(all);
            if(fin != null && fin.get(ftype) != null && fin.get(ftype).equals(fvalue))
            {
                freturn.add((T) fin);
            }
        }
        
        return freturn;
    }
    
    @SuppressWarnings("unchecked")
    public AttributeManager<T> getAll(int ftype, Object fvalue, int ftype2, Object fvalue2)
    {
        AttributeManager<Object> fin;
        AttributeManager<T> freturn = new AttributeManager<T>();
        
        for(int all = 0; all < size(); ++all)
        {
            fin = (AttributeManager<Object>) 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({ "rawtypes", "unchecked" })
    public T get(int ftype, int finside, Object fvalue)
    {
        AttributeManager ftmp;
        
        for(int all = 0; all < 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() > 0)
            return attributes.remove(0);
        return null;
    }
    
    public T last()
    {
        return get(size() - 1);
    }
    
    public T pop()
    {
        if(size() > 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() > ftype)
        {
            attributes.set(ftype, fvalue);
        }
        else
        {
            for(int all = size(); all < ftype; ++all)
                add(null);
            add(fvalue);
        }
    }
    
    @SuppressWarnings({ "rawtypes", "unchecked" })
	public void setAll(int ftype, Object fvalue)
    {
    	for(int fall = 0; fall < size(); ++fall)
    	{
    		((AttributeManager) get(fall)).set(ftype, fvalue);
    	}
    }
    
    //Remove
    public void remove(int ftype) //Remove object by type
    {
        if(size() > ftype)
        {
            if(ftype == size() - 1)
            {
                attributes.remove(ftype);
                
                for(int all = ftype - 1; all >= 0; --all)
                {
                    if(get(all) == null)
                    {
                        attributes.remove(all);
                    }
                    else break;
                }
            }
            else attributes.set(ftype, null);
        }
        else debug("Invalid position on remove.");
    }
    
    @SuppressWarnings("rawtypes")
    public void remove(int ftype, Object fvalue)
    {
        for(int all = 0; all < size(); ++all)
        {
            if(((AttributeManager) get(all)).get(ftype).equals(fvalue))
            {
                attributes.remove(all);
                return;
            }
        }
        debug("Unale te remove.");
    }
    
    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 = "";
        for(int all = 0; all < attributes.size(); ++all)
        {
            if(fresult.length() > 0)
                fresult = fresult + " ";
            fresult = fresult + get(all);
        }
        
        return fresult;
    }
    
    //Extra
    public ArrayList<T> array()
    {
        return this.attributes;
    }
}

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

3 Respostas

U

Muito bom parabéns :wink:

joaosouza

Bem interessante…

Valew pela contribuição !!

PeJuGe

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

Criado 26 de junho de 2011
Ultima resposta 29 de jul. de 2011
Respostas 3
Participantes 3