Search in sources :

Example 1 with PriorityParameterListener

use of com.biglybt.core.config.PriorityParameterListener in project BiglyBT by BiglySoftware.

the class ConfigurationManager method addParameterListener.

public void addParameterListener(String parameter, ParameterListener new_listener) {
    if (parameter == null || new_listener == null) {
        return;
    }
    synchronized (parameterListenerz) {
        ParameterListener[] listeners = parameterListenerz.get(parameter);
        if (listeners == null) {
            parameterListenerz.put(parameter, new ParameterListener[] { new_listener });
        } else {
            ParameterListener[] new_listeners = new ParameterListener[listeners.length + 1];
            if (Constants.IS_CVS_VERSION && listeners.length > 100) {
                Debug.out(parameter);
            }
            int pos;
            if (new_listener instanceof PriorityParameterListener) {
                new_listeners[0] = new_listener;
                pos = 1;
            } else {
                new_listeners[listeners.length] = new_listener;
                pos = 0;
            }
            for (int i = 0; i < listeners.length; i++) {
                ParameterListener existing_listener = listeners[i];
                if (existing_listener == new_listener) {
                    return;
                }
                new_listeners[pos++] = existing_listener;
            }
            if (DEBUG_PARAMETER_LISTENERS) {
                System.out.println(parameter + "->" + new_listeners.length);
            }
            parameterListenerz.put(parameter, new_listeners);
        }
    }
}
Also used : PriorityParameterListener(com.biglybt.core.config.PriorityParameterListener) ParameterListener(com.biglybt.core.config.ParameterListener) PriorityParameterListener(com.biglybt.core.config.PriorityParameterListener)

Example 2 with PriorityParameterListener

use of com.biglybt.core.config.PriorityParameterListener in project BiglyBT by BiglySoftware.

the class ConfigurationManager method addWeakParameterListener.

public void addWeakParameterListener(String parameter, ParameterListener new_listener) {
    if (parameter == null || new_listener == null) {
        return;
    }
    synchronized (weakParameterListenerz) {
        List<WeakReference<ParameterListener>> listeners = weakParameterListenerz.get(parameter);
        if (listeners == null) {
            listeners = new ArrayList<>(1);
            listeners.add(new WeakReference<ParameterListener>(new_listener));
            weakParameterListenerz.put(parameter, listeners);
        } else {
            if (Constants.IS_CVS_VERSION && listeners.size() > 100) {
                Debug.out(parameter);
            }
            for (Iterator<WeakReference<ParameterListener>> iterator = listeners.iterator(); iterator.hasNext(); ) {
                ParameterListener listener = iterator.next().get();
                if (listener == null) {
                    iterator.remove();
                }
                if (listener == new_listener) {
                    return;
                }
            }
            WeakReference<ParameterListener> weakRef = new WeakReference<>(new_listener);
            if (new_listener instanceof PriorityParameterListener) {
                listeners.add(0, weakRef);
            } else {
                listeners.add(weakRef);
            }
        }
    }
}
Also used : PriorityParameterListener(com.biglybt.core.config.PriorityParameterListener) WeakReference(java.lang.ref.WeakReference) ParameterListener(com.biglybt.core.config.ParameterListener) PriorityParameterListener(com.biglybt.core.config.PriorityParameterListener)

Example 3 with PriorityParameterListener

use of com.biglybt.core.config.PriorityParameterListener in project BiglyBT by BiglySoftware.

the class ConfigurationManager method notifyParameterListeners.

private void notifyParameterListeners(String parameter) {
    ParameterListener[] listeners;
    synchronized (parameterListenerz) {
        listeners = parameterListenerz.get(parameter);
    }
    List<ParameterListener> listListeners = null;
    synchronized (weakParameterListenerz) {
        List<WeakReference<ParameterListener>> temp = weakParameterListenerz.get(parameter);
        if (temp != null) {
            for (Iterator<WeakReference<ParameterListener>> iterator = temp.iterator(); iterator.hasNext(); ) {
                ParameterListener listener = iterator.next().get();
                if (listener == null) {
                    iterator.remove();
                } else {
                    if (listListeners == null) {
                        listListeners = new ArrayList<>(temp.size() + (listeners == null ? 0 : listeners.length));
                    }
                    if (listeners != null && !(listener instanceof PriorityParameterListener)) {
                        // listeners might have PriorityParameterListener, so append
                        // them before the first non-Priority one
                        Collections.addAll(listListeners, listeners);
                        listeners = null;
                    }
                    listListeners.add(listener);
                }
            }
        }
    }
    if (listListeners != null) {
        for (ParameterListener listener : listListeners) {
            if (listener != null) {
                try {
                    listener.parameterChanged(parameter);
                } catch (Throwable e) {
                    Debug.printStackTrace(e);
                }
            }
        }
    }
    if (listeners != null) {
        for (ParameterListener listener : listeners) {
            if (listener != null) {
                try {
                    listener.parameterChanged(parameter);
                } catch (Throwable e) {
                    Debug.printStackTrace(e);
                }
            }
        }
    }
}
Also used : PriorityParameterListener(com.biglybt.core.config.PriorityParameterListener) WeakReference(java.lang.ref.WeakReference) ParameterListener(com.biglybt.core.config.ParameterListener) PriorityParameterListener(com.biglybt.core.config.PriorityParameterListener)

Aggregations

ParameterListener (com.biglybt.core.config.ParameterListener)3 PriorityParameterListener (com.biglybt.core.config.PriorityParameterListener)3 WeakReference (java.lang.ref.WeakReference)2