All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.viaoa.hub.HubUniqueIndex Maven / Gradle / Ivy

package com.viaoa.hub;

import java.util.concurrent.ConcurrentHashMap;

import com.viaoa.util.OAPropertyPath;

/**
 * create an index for a Hub.
 * @author vvia
 */
public class HubUniqueIndex {
    
    private final Hub hub;
    private final String property;
    private final boolean bCaseSensitive;
    private final HubListener listener;
    private final ConcurrentHashMap hm = new ConcurrentHashMap();
    private final OAPropertyPath propertyPath;
    
    public HubUniqueIndex(Hub hub, String prop) {
        this(hub, prop, false);
    }
    
    public HubUniqueIndex(Hub hub, String prop, boolean bCaseSensitive) {
        this.hub = hub;
        this.property = prop;
        this.bCaseSensitive = bCaseSensitive;
        this.propertyPath = new OAPropertyPath(hub.getObjectClass(), prop);
        
        listener = new HubListenerAdapter() {
            @Override
            public void afterPropertyChange(HubEvent e) {
                if (e == null || !property.equalsIgnoreCase(e.getPropertyName())) return;
                TYPE object = e.getObject();
                if (object == null) return;
                Object old = e.getOldValue();
                if (old != null) {
                    if (!HubUniqueIndex.this.bCaseSensitive && old instanceof String) old = ((String)old).toUpperCase(); 
                    hm.remove(old);
                }
                
                Object value = propertyPath.getValue(object);
                if (value != null) {
                    if (!HubUniqueIndex.this.bCaseSensitive && value instanceof String) value = ((String)value).toUpperCase(); 
                    hm.put(value, object);
                }
            }
            @Override
            public void afterAdd(HubEvent e) {
                add(e.getObject());
            }
            @Override
            public void afterInsert(HubEvent e) {
                add(e.getObject());
            }
            @Override
            public void afterRemove(HubEvent e) {
                if (e == null) return;
                TYPE object = e.getObject();
                if (object == null) return;
                Object value = propertyPath.getValue(object);
                if (value != null) {
                    if (!HubUniqueIndex.this.bCaseSensitive && value instanceof String) value = ((String)value).toUpperCase(); 
                    hm.remove(value);
                }
            }
            @Override
            public void onNewList(HubEvent e) {
                HubUniqueIndex.this.onNewList();
            }
            @Override
            public void afterRemoveAll(HubEvent e) {
                hm.clear();
            }
        };
        hub.addHubListener(listener);
    }

    private void add(TYPE object) {
        if (object == null) return;
        Object value = propertyPath.getValue(object);
        if (value != null) {
            if (!HubUniqueIndex.this.bCaseSensitive && value instanceof String) value = ((String)value).toUpperCase(); 
            hm.put(value, object);
        }
    }
    private void onNewList() {
        hm.clear();
        for (TYPE object : HubUniqueIndex.this.hub) {
            add(object);
        }
    }
    
    
    @Override
    protected void finalize() throws Throwable {
        close();
        super.finalize();
    }
    public void close() {
        hub.removeHubListener(listener);
    }

    public TYPE get(Object id) {
        if (id == null) return null;
        if (!bCaseSensitive && id instanceof String) id = ((String)id).toUpperCase(); 
        return hm.get(id);
    }
    
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy