26
26
import java.util.HashMap;
27
27
import java.util.HashSet;
28
import java.util.Locale;
29
30
public class ContactsCache
32
* Information that can be used to identify a contact within the cache
33
* A thing that can be used to identify (or lookup) a contact within the
34
* contacts cache. It is not a reference to a cache entry and may not
35
* identify an existing contact in the cache.
34
static public class CacheIdentifier
37
public static class CacheIdentifier
37
40
NONE, NAME, ORGANISATION, PRIMARY_NUMBER, PRIMARY_EMAIL }
51
* Obtain a cache identifier. This routine is designed to be as robust
52
* as possible (in terms of bad or null detail values), and to return
53
* null when a cache identifier can not be created.
54
* @param type the detail type
55
* @param detail the detail
56
* @return the cache identifier, or null
58
public static CacheIdentifier factory( Type type, String detail )
62
case NAME: detail = normaliseName( detail ); break;
63
case ORGANISATION: detail = normaliseOrganisation( detail ); break;
64
case PRIMARY_NUMBER: detail = normalisePhoneNumber( detail ); break;
65
case PRIMARY_EMAIL: detail = normaliseEmailAddress( detail ); break;
68
if( detail == null ) return null;
69
return new CacheIdentifier( type, detail );
73
* Obtain a cache identifier from contact data. This routine is
74
* designed to be as robust as possible and may return null when a cache
75
* identifier can not be created.
76
* @param contact the contact data
77
* @return the cache identifier, or null
79
public static CacheIdentifier factory( Importer.ContactData contact )
81
CacheIdentifier ret = null;
83
if( contact.hasName() )
84
ret = factory( CacheIdentifier.Type.NAME,
86
if( ret == null && contact.hasPrimaryOrganisation() )
87
ret = factory( CacheIdentifier.Type.ORGANISATION,
88
contact.getPrimaryOrganisation() );
89
if( ret == null && contact.hasPrimaryNumber() )
90
ret = factory( CacheIdentifier.Type.PRIMARY_NUMBER,
91
contact.getPrimaryNumber() );
92
if( ret == null && contact.hasPrimaryEmail() )
93
ret = factory( CacheIdentifier.Type.PRIMARY_EMAIL,
94
contact.getPrimaryEmail() );
47
99
protected CacheIdentifier( Type type, String detail )
80
132
= new HashMap< Long, HashSet< String > >();
81
133
private HashMap< Long, HashSet< String > > _contactOrganisations
82
134
= new HashMap< Long, HashSet< String > >();
84
public static CacheIdentifier createIdentifier(
85
Importer.ContactData contact )
87
if( contact.hasName() ) {
88
String name = normaliseName( contact.getName() );
90
return new CacheIdentifier(
91
CacheIdentifier.Type.NAME, name );
94
if( contact.hasPrimaryOrganisation() ) {
95
String organisation = normaliseOrganisation(
96
contact.getPrimaryOrganisation() );
97
if( organisation != null )
98
return new CacheIdentifier(
99
CacheIdentifier.Type.ORGANISATION, organisation );
102
if( contact.hasPrimaryNumber() ) {
103
String number = normalisePhoneNumber( contact.getPrimaryNumber() );
105
return new CacheIdentifier(
106
CacheIdentifier.Type.PRIMARY_NUMBER, number );
109
if( contact.hasPrimaryEmail() ) {
110
String email = normaliseEmailAddress( contact.getPrimaryEmail() );
112
return new CacheIdentifier(
113
CacheIdentifier.Type.PRIMARY_EMAIL, email );
135
private HashMap< Long, HashSet< String > > _contactNotes
136
= new HashMap< Long, HashSet< String > >();
119
138
public boolean canLookup( CacheIdentifier identifier )
121
140
return lookup( identifier ) != null;
144
* Retrieve the contact id of a contact identified by the specified cache
145
* identifier, if it exists.
146
* @param identifier the cache identifier
147
* @return a contact id, or null
124
149
public Long lookup( CacheIdentifier identifier )
126
151
switch( identifier.getType() )
166
* Remove any cache entry that is identified by the cache identifier.
167
* @param identifier the cache identifier
168
* @return the contact id of the contact that was removed, or null
140
170
public Long removeLookup( CacheIdentifier identifier )
142
172
switch( identifier.getType() )
187
* Add a lookup from a contact identifier to a contact id to the cache.
188
* @param identifier the cache identifier
189
* @param id teh contact id
156
191
public void addLookup( CacheIdentifier identifier, Long id )
158
193
switch( identifier.getType() )
211
* Remove any data that is associated with an contact id.
175
214
public void removeAssociatedData( Long id )
177
216
_contactNumbers.remove( id );
178
217
_contactEmails.remove( id );
179
218
_contactAddresses.remove( id );
180
219
_contactOrganisations.remove( id );
220
_contactNotes.remove( id );
183
223
public boolean hasAssociatedNumber( Long id, String number )
199
239
set = new HashSet< String >();
200
240
_contactNumbers.put( id, set );
202
set.add( normalisePhoneNumber( number ) );
205
245
public boolean hasAssociatedEmail( Long id, String email )
208
248
if( email == null ) return false;
210
250
HashSet< String > set = _contactEmails.get( id );
211
return set != null && set.contains( normaliseEmailAddress( email ) );
251
return set != null && set.contains( email );
214
254
public void addAssociatedEmail( Long id, String email )
221
261
set = new HashSet< String >();
222
262
_contactEmails.put( id, set );
224
set.add( normaliseEmailAddress( email ) );
227
267
public boolean hasAssociatedAddress( Long id, String address )
230
270
if( address == null ) return false;
232
272
HashSet< String > set = _contactAddresses.get( id );
233
return set != null && set.contains( normaliseAddress( address ) );
273
return set != null && set.contains( address );
236
276
public void addAssociatedAddress( Long id, String address )
243
283
set = new HashSet< String >();
244
284
_contactAddresses.put( id, set );
246
set.add( normaliseAddress( address ) );
249
289
public boolean hasAssociatedOrganisation( Long id, String organisation )
252
292
if( organisation == null ) return false;
254
294
HashSet< String > set = _contactOrganisations.get( id );
255
return set != null && set.contains(
256
normaliseOrganisation( organisation ) );
295
return set != null && set.contains( organisation );
259
298
public void addAssociatedOrganisation( Long id, String organisation )
266
305
set = new HashSet< String >();
267
306
_contactOrganisations.put( id, set );
269
set.add( normaliseOrganisation( organisation ) );
308
set.add( organisation );
311
public boolean hasAssociatedNote( Long id, String note )
313
note = normaliseNote( note );
314
if( note == null ) return false;
316
HashSet< String > set = _contactNotes.get( id );
317
return set != null && set.contains( note );
320
public void addAssociatedNote( Long id, String note )
322
note = normaliseNote( note );
323
if( note == null ) return;
325
HashSet< String > set = _contactNotes.get( id );
327
set = new HashSet< String >();
328
_contactNotes.put( id, set );
272
333
static public String normaliseName( String name )
286
347
static public String normaliseEmailAddress( String email )
288
349
if( email == null ) return null;
289
email = email.trim().toLowerCase();
350
email = email.trim().toLowerCase( Locale.US );
290
351
return email.length() > 0? email : null;
303
364
address = address.trim();
304
365
return address.length() > 0? address : null;
368
static public String normaliseNote( String note )
370
if( note == null ) return null;
372
return note.length() > 0? note : null;