Source code

    001/*
    002 * Licensed to the Apache Software Foundation (ASF) under one or more
    003 * contributor license agreements.  See the NOTICE file distributed with
    004 * this work for additional information regarding copyright ownership.
    005 * The ASF licenses this file to You under the Apache License, Version 2.0
    006 * (the "License"); you may not use this file except in compliance with
    007 * the License.  You may obtain a copy of the License at
    008 *
    009 *      http://www.apache.org/licenses/LICENSE-2.0
    010 *
    011 *  Unless required by applicable law or agreed to in writing, software
    012 *  distributed under the License is distributed on an "AS IS" BASIS,
    013 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    014 *  See the License for the specific language governing permissions and
    015 *  limitations under the License.
    016 */
    017package org.apache.bcel.verifier;
    018
    019import java.util.ArrayList;
    020import java.util.List;
    021import java.util.Set;
    022import java.util.TreeSet;
    023
    024import javax.swing.ListModel;
    025import javax.swing.event.ListDataEvent;
    026import javax.swing.event.ListDataListener;
    027
    028import org.apache.commons.lang3.ArrayUtils;
    029
    030/**
    031 * This class implements an adapter; it implements both a Swing ListModel and a VerifierFactoryObserver.
    032 */
    033public class VerifierFactoryListModel implements VerifierFactoryObserver, ListModel<String> {
    034
    035    private final List<ListDataListener> listeners = new ArrayList<>();
    036    private final Set<String> cache = new TreeSet<>();
    037
    038    public VerifierFactoryListModel() {
    039        VerifierFactory.attach(this);
    040        update(null); // fill cache.
    041    }
    042
    043    @Override
    044    public synchronized void addListDataListener(final ListDataListener l) {
    045        listeners.add(l);
    046    }
    047
    048    @Override
    049    public synchronized String getElementAt(final int index) {
    050        return cache.toArray(ArrayUtils.EMPTY_STRING_ARRAY)[index];
    051    }
    052
    053    @Override
    054    public synchronized int getSize() {
    055        return cache.size();
    056    }
    057
    058    @Override
    059    public synchronized void removeListDataListener(final ListDataListener l) {
    060        listeners.remove(l);
    061    }
    062
    063    @Override
    064    public synchronized void update(final String s) {
    065        final Verifier[] verifiers = VerifierFactory.getVerifiers();
    066        final int verifierLen = verifiers.length;
    067        cache.clear();
    068        for (final Verifier verifier : verifiers) {
    069            cache.add(verifier.getClassName());
    070        }
    071        for (final ListDataListener listener : listeners) {
    072            listener.contentsChanged(new ListDataEvent(this, ListDataEvent.CONTENTS_CHANGED, 0, verifierLen - 1));
    073        }
    074    }
    075
    076}