NotesWhat is notes.io?

Notes brand slogan

Notes - notes.io

package pt.ipleiria.estg.dei.contacts.model;

import java.util.LinkedList;

import javax.crypto.Cipher;

import pt.ipleiria.estg.dei.colecoes.ComparacaoLimite;
import pt.ipleiria.estg.dei.colecoes.Iterador;
import pt.ipleiria.estg.dei.colecoes.ListaSimplesCircularBaseLimite;
import pt.ipleiria.estg.dei.colecoes.TabelaHashPorListas;
import pt.ipleiria.estg.dei.colecoes.TabelaHashPorSondagem;
import pt.ipleiria.estg.dei.colecoes.TabelaHashPorSondagemHashingDuplo;
import pt.ipleiria.estg.dei.contacts.colections.Filtro;
import pt.ipleiria.estg.dei.contacts.colections.ListaSimplesCircularBaseLimiteFiltravel;


public enum ContactManager {
INSTANCE;

static public String getDomain(String email){
String[] domain;

String delimiter = "@";

domain = email.split(delimiter);


return domain[1].toString();

}



public Iterable<Long> getAllIds() {
LinkedList<Long> ids = new LinkedList<>();
/*for (Contact contact : getAll(OrderBy.ID_ASC)) { //podemos fazer porque temos lista
ids.add(contact.getId());
}*/
Iterador<Long> it = hashContactsById.iteradorChaves();
while (it.podeAvancar()){ // se não tivermos lista
ids.add(it.avancar());
}

return ids;
}

public Iterable<String> getAllDomains() {
LinkedList<String> ids = new LinkedList<>();
Iterador<String> it = hashContactsDomain.iteradorChaves();
while (it.podeAvancar()){ // se não tivermos lista
ids.add(it.avancar());
}

return ids;
}

public Iterable<String> getAllDomains(int year) {
LinkedList<String> domains = new LinkedList<>();
TabelaHashPorSondagemHashingDuplo<String, ListaSimplesCircularBaseLimite<Contact>> hash
= hashContactsByYearByDomain.consultar(year);

if(hash == null){
return domains;
}
Iterador<String> it = hash.iteradorChaves();
while (it.podeAvancar()){ // se não tivermos lista
domains.add(it.avancar());
}

return domains;
}



public Iterable<Contact> getAll(String domain){
ListaSimplesCircularBaseLimite<Contact> lista = hashContactsDomain.consultar(domain);
if(lista == null){
lista = new ListaSimplesCircularBaseLimite<Contact>(CompareContactIdsByAscendingOrder.INSTANCE);
}
return lista;
}


public Iterable<Contact> getAll(int year){ //para devolver os contacts
ListaSimplesCircularBaseLimite<Contact> lista = hashContactsYear.consultar(year);
if(lista == null){
lista = new ListaSimplesCircularBaseLimite<Contact>(CompareContactIdsByAscendingOrder.INSTANCE);
}
return lista;
}


public Iterable<Integer> getAllYears() { //devolve todos os anos
LinkedList<Integer> years = new LinkedList<>();
Iterador<Integer> it = hashContactsYear.iteradorChaves();
while (it.podeAvancar()){ // se não tivermos lista
years.add(it.avancar());
}

return years;
}

public Iterable<Contact> getAll(int year, String domain){ //para devolver os contacts
TabelaHashPorSondagemHashingDuplo<String, ListaSimplesCircularBaseLimite<Contact>> hash =
hashContactsByYearByDomain.consultar(year);
ListaSimplesCircularBaseLimite<Contact> lista = new ListaSimplesCircularBaseLimite<Contact>(CompareContactIdsByAscendingOrder.INSTANCE);
if (hash == null){
return lista;
}
lista = hash.consultar(domain);

if(lista == null){
lista = new ListaSimplesCircularBaseLimite<Contact>(CompareContactIdsByAscendingOrder.INSTANCE);
}
return lista;
}


public enum OrderBy {
ID_ASC(CompareContactIdsByAscendingOrder.INSTANCE),
NAME_DESC(CompareContactNamesByDescendingOrder.INSTANCE);

public final ComparacaoLimite<Contact> COMPARATOR;

OrderBy(ComparacaoLimite<Contact> comparator) {
this.COMPARATOR = comparator;
}
}


private ListaSimplesCircularBaseLimiteFiltravel<Contact>[] contacts;

private TabelaHashPorSondagemHashingDuplo<Long, Contact> hashContactsById; // não é preciso lista porque há vários
private TabelaHashPorSondagemHashingDuplo<String, ListaSimplesCircularBaseLimite<Contact>> hashContactsDomain; //é uma lista porque há vários
private TabelaHashPorSondagemHashingDuplo<String, Contact> hashContactsEmail;
private TabelaHashPorSondagemHashingDuplo<Integer, ListaSimplesCircularBaseLimite<Contact>> hashContactsYear;
private TabelaHashPorSondagemHashingDuplo<Integer, TabelaHashPorSondagemHashingDuplo<String,
ListaSimplesCircularBaseLimite<Contact>>> hashContactsByYearByDomain;

ContactManager() {
contacts = new ListaSimplesCircularBaseLimiteFiltravel[OrderBy.values().length];

for (OrderBy order : OrderBy.values()) {
contacts[order.ordinal()] =
new ListaSimplesCircularBaseLimiteFiltravel<Contact>(order.COMPARATOR);
}

hashContactsById = new TabelaHashPorSondagemHashingDuplo<>(100, new HashingNumber<Long>(),
new HashingNumberIncrement<Long>(100));

hashContactsDomain = new TabelaHashPorSondagemHashingDuplo<>(100, new HashingString(),
new HashingStringIncrement(100));

hashContactsEmail = new TabelaHashPorSondagemHashingDuplo<>(100, new HashingString(), new HashingStringIncrement(100));


hashContactsYear = new TabelaHashPorSondagemHashingDuplo<>(100, new HashingNumber<Integer>(),
new HashingNumberIncrement<Integer>(100));

hashContactsByYearByDomain = new TabelaHashPorSondagemHashingDuplo<>(100, new HashingNumber<Integer>(),
new HashingNumberIncrement<Integer>(100));



addExampleContacts();
}

private void addExampleContacts() {
add(new Contact(12345, "Carlos Urbano", 1973, "[email protected]"));
add(new Contact(65432, "Ana Silva", 1981, "[email protected]"));
add(new Contact(56462, "Joaquim Correia", 2000, "[email protected]"));
add(new Contact(43526, "Anibal Pereira", 2000, "[email protected]"));
add(new Contact(21342, "Manuel José", 1973, "[email protected]"));
add(new Contact(67848, "Maria Joaquina", 2000, "[email protected]"));
add(new Contact(84753, "Daniela Alves", 1981, "[email protected]"));
}

public void add(Contact c) { //garante que o contacto tem um ID unico
if (hashContactsById.contem(c.getId())) {
throw new RuntimeException("Contact with ID: " + c.getId() + " already exists !!!");
}

if (hashContactsEmail.contem(c.getEmail())) {
throw new RuntimeException("Contact with EMAIL: " + c.getEmail() + " already exists !!!");
}

for (OrderBy order : OrderBy.values()) {
contacts[order.ordinal()].inserir(c);
}

hashContactsById.inserir(c.getId(), c);

//hashTable domain
String domain = getDomain(c.getEmail());
ListaSimplesCircularBaseLimite<Contact> list = hashContactsDomain.consultar(domain);
if(list == null){
list = new ListaSimplesCircularBaseLimite<Contact>(CompareContactIdsByAscendingOrder.INSTANCE);
hashContactsDomain.inserir(domain, list);
}
list.inserir(c);

//hashTable email
hashContactsEmail.inserir(c.getEmail(), c);

//hashTable ano, temos de veririficar se já existe
list = hashContactsYear.consultar(c.getYear()); //consultar para ver se já existe
if(list == null){ // se estiver a null, inicealiza-se uma nova

list = new ListaSimplesCircularBaseLimite<Contact>(CompareContactIdsByAscendingOrder.INSTANCE);
hashContactsYear.inserir(c.getYear(), list); //chave é o ano e temos de inserir a lista na hashTable

}

list.inserir(c);

//hashTable yearDomain
TabelaHashPorSondagemHashingDuplo<String, ListaSimplesCircularBaseLimite<Contact>> hash = hashContactsByYearByDomain.consultar(c.getYear());
if(hash == null){
hash = new TabelaHashPorSondagemHashingDuplo<>(100, new HashingString(),
new HashingStringIncrement(100));
hashContactsByYearByDomain.inserir(c.getYear(), hash);
}
list = hash.consultar(domain);
if(list == null){
list = new ListaSimplesCircularBaseLimite<Contact>(CompareContactIdsByAscendingOrder.INSTANCE);
hash.inserir(domain, list);
}

list.inserir(c);

}

public void remove(Contact c) {
for (OrderBy order : OrderBy.values()) {
contacts[order.ordinal()].remover(c);
}

hashContactsById.remover(c.getId());

//hashTable domain
String domain = getDomain(c.getEmail());
ListaSimplesCircularBaseLimite<Contact> list = hashContactsDomain.consultar(domain);
if (list != null) {
list.remover(c);
}
//hashTable email
hashContactsEmail.remover(c.getEmail());

//hashTable year
list = hashContactsYear.consultar(c.getYear());
if (list != null) {
list.remover(c);
}


//hashTable yearDomain
TabelaHashPorSondagemHashingDuplo<String, ListaSimplesCircularBaseLimite<Contact>> hash = hashContactsByYearByDomain.consultar(c.getYear());
if (hash != null) {
list = hash.consultar(domain);
if (list != null) {
list.remover(c);
}

}
}

public Contact get(OrderBy order, int index) {
return contacts[order.ordinal()].consultar(index);
}

public Contact getById(long id) {
return hashContactsById.consultar(id);
}

public Contact getByEmail(String email){
return hashContactsEmail.consultar(email);
}



public Iterable<Contact> getAll(OrderBy order) {
return contacts[order.ordinal()];
}

@Override
public String toString() {
StringBuilder result = new StringBuilder("");

for (OrderBy order : OrderBy.values()) {
result.append("=========== ").append(order).append("n");
result.append(contacts[order.ordinal()]);
}

result.append("nHashContactsByIdn").append(hashContactsById);
return result.toString();
}

public void update(Contact oldContact, Contact newContact) {
if (oldContact != null) {
LinkedList<Integer> listasOndeRemovi = new LinkedList<>();

for (OrderBy order : OrderBy.values()) {
if (order.COMPARATOR.comparar(oldContact, newContact) != 0) {
contacts[order.ordinal()].removerPorReferencia(oldContact);
listasOndeRemovi.add(order.ordinal());
}
}
if (oldContact.getId() != newContact.getId()) {
hashContactsById.remover(oldContact.getId());
}
if(oldContact.getEmail() != newContact.getEmail()){
hashContactsEmail.remover(oldContact.getEmail());
}


oldContact.copyFrom(newContact);

for (Integer i : listasOndeRemovi) {
contacts[i].inserir(oldContact);
}
}
}

public void filterBy(Filtro<Contact> filtro) {
for (OrderBy order : OrderBy.values()) {
contacts[order.ordinal()].setFiltro(filtro);
}
}


}



     
 
what is notes.io
 

Notes.io is a web-based application for taking notes. You can take your notes and share with others people. If you like taking long notes, notes.io is designed for you. To date, over 8,000,000,000 notes created and continuing...

With notes.io;

  • * You can take a note from anywhere and any device with internet connection.
  • * You can share the notes in social platforms (YouTube, Facebook, Twitter, instagram etc.).
  • * You can quickly share your contents without website, blog and e-mail.
  • * You don't need to create any Account to share a note. As you wish you can use quick, easy and best shortened notes with sms, websites, e-mail, or messaging services (WhatsApp, iMessage, Telegram, Signal).
  • * Notes.io has fabulous infrastructure design for a short link and allows you to share the note as an easy and understandable link.

Fast: Notes.io is built for speed and performance. You can take a notes quickly and browse your archive.

Easy: Notes.io doesn’t require installation. Just write and share note!

Short: Notes.io’s url just 8 character. You’ll get shorten link of your note when you want to share. (Ex: notes.io/q )

Free: Notes.io works for 12 years and has been free since the day it was started.


You immediately create your first note and start sharing with the ones you wish. If you want to contact us, you can use the following communication channels;


Email: [email protected]

Twitter: http://twitter.com/notesio

Instagram: http://instagram.com/notes.io

Facebook: http://facebook.com/notesio



Regards;
Notes.io Team

     
 
Shortened Note Link
 
 
Looding Image
 
     
 
Long File
 
 

For written notes was greater than 18KB Unable to shorten.

To be smaller than 18KB, please organize your notes, or sign in.