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

src.samples.java.ex.PSC_Sample Maven / Gradle / Ivy

Go to download

An auxiliary findbugs.sourceforge.net plugin for java bug detectors that fall outside the narrow scope of detectors to be packaged with the product itself.

The newest version!
package ex;

import java.io.BufferedReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.StringTokenizer;
import java.util.TreeSet;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.google.common.collect.Sets;

public class PSC_Sample {

    private static final Pattern REGEX_TXN = Pattern.compile("((.*?))");

    public void testPSC(List samples) {
        Set names = new HashSet<>();
        for (PSC_Sample s : samples) {
            names.add(s.toString());
        }
    }

    public void testPSCMaps(Map input) {
        Map output = new HashMap<>();
        for (Map.Entry entry : input.entrySet()) {
            output.put(entry.getKey().intern(), entry.getValue());
        }
    }

    public void testPSCEnumerated() {
        Set commonWords = new HashSet<>();
        commonWords.add("a");
        commonWords.add("an");
        commonWords.add("the");
        commonWords.add("by");
        commonWords.add("of");
        commonWords.add("and");
        commonWords.add("or");
        commonWords.add("in");
        commonWords.add("with");
        commonWords.add("my");
        commonWords.add("I");
        commonWords.add("on");
        commonWords.add("over");
        commonWords.add("under");
        commonWords.add("it");
        commonWords.add("they");
        commonWords.add("them");
    }

    public List testAddAllToCtor(List l) {
        List ll = new ArrayList<>();
        ll.addAll(l);

        ll.add("FooBar");
        return ll;
    }

    public List testGuavaLists(List ii) {
        List ss = Lists.newArrayList();

        for (Integer i : ii) {
            ss.add(String.valueOf(i));
        }

        return ss;
    }

    public Set testGuavaSets(List ii) {
        Set ss = Sets.newHashSet();

        for (Integer i : ii) {
            ss.add(String.valueOf(i));
        }

        return ss;
    }

    public Map testGuavaMaps(List ii) {
        Map ss = Maps.newHashMap();

        for (Integer i : ii) {
            ss.put(String.valueOf(i), i);
        }

        return ss;
    }

    public String testNaiveSizing(Collection cc) {
        Map mm = new HashMap<>(cc.size());
        for (String c : cc) {
            mm.put(c, c);
        }

        Set ss = new HashSet<>(cc.size());
        for (String c : cc) {
            ss.add(c);
        }

        return mm.toString() + " - " + ss.toString();

    }

    public void fpDontHaveCollectionForSizing(Iterator it) {
        Set ad = new TreeSet<>();
        while (it.hasNext()) {
            ad.add(it.next());
        }
    }

    public void fpConditionalInLoop(Set source) {
        List dest = new ArrayList<>();
        for (String s : source) {
            if (s.length() > 0) {
                dest.add(s);
            }
        }
    }

    public List fpAddSubCollection(Map> s) {
        List l = new ArrayList<>();
        for (Map.Entry> entry : s.entrySet()) {
            l.add(entry.getKey());
            l.addAll(entry.getValue());
        }
        return l;
    }

    public void fpSwitchInLoop(Set source) {
        List dest = new ArrayList<>();
        for (Integer s : source) {
            switch (s.intValue()) {
            case 0:
                dest.add(s);
                break;
            case 1:
                dest.remove(s);
                break;
            }
        }
    }

    public void fpAllocationInLoop(Map source) {
        Map> dest = new HashMap<>();

        for (Map.Entry entry : source.entrySet()) {

            List l = new ArrayList<>();
            l.add(entry.getValue());
            dest.put(entry.getKey(), l);
        }
    }

    public List fpUnknownSrcSize(BufferedReader br) throws IOException {
        List l = new ArrayList<>();
        String line;
        while ((line = br.readLine()) != null) {
            l.add(line);
        }

        return l;
    }

    public List fpPSCInCatchBlock(List src) {
        List exceptions = new ArrayList<>();

        for (String s : src) {
            try {
                s = s.substring(1000, 1001);

            } catch (IndexOutOfBoundsException e) {
                exceptions.add(e);
            }
        }

        List exceptions2 = new ArrayList<>();

        for (String s : src) {
            try {
                s = s.substring(1000, 1001);
                if (s == null) {
                    return null;
                }
            } catch (IndexOutOfBoundsException e) {
                exceptions2.add(e);
            }
        }

        return exceptions;
    }

    public void fpNoAllocation(List ss, List ii) {
        for (Integer i : ii) {
            ss.add(ii + "");
        }
    }

    public List fpWithEnumeration247(Enumeration e) {
        List result = new ArrayList<>();
        while (e.hasMoreElements()) {
            result.add("A" + e.nextElement());
        }

        return result;
    }

    public List fpTokenizer(StringTokenizer st) {
        List result = new ArrayList<>();
        while (st.hasMoreTokens()) {
            result.add(st.nextToken());
        }

        return result;
    }

    public List fpNoSizedSource(Iterator it) {
        List result = new ArrayList<>();
        while (it.hasNext()) {
            result.add(it.next());
        }

        return result;
    }

    public List fpStreamSource249(BooReader br) throws IOException, ClassNotFoundException {
        List result = new ArrayList<>();
        Object o;
        while (br.tokenType() != BooReader.BooTokenType.END) {
            result.add(br.nextToken());
        }

        return result;
    }

    public void fpInitMultipleOnSameRef(boolean a, boolean b) {
        Set immutable1;
        Set immutable2;

        Set s = new HashSet<>();
        s.add("A1");
        s.add("A2");
        s.add("A3");
        s.add("A4");
        s.add("A5");
        s.add("A6");
        s.add("A7");
        s.add("A8");
        s.add("A9");
        s.add("A10");
        s.add("A11");
        s.add("A12");
        s.add("A13");
        s.add("A14");
        s.add("A15");
        immutable1 = Collections.unmodifiableSet(s);

        s = new HashSet<>();
        s.add("B1");
        s.add("B2");
        s.add("B3");
        s.add("B4");
        s.add("B5");
        s.add("B6");
        s.add("B7");
        s.add("B8");
        immutable2 = Collections.unmodifiableSet(s);
    }

    public List fpMatcher(Matcher m) {
        List ss = new ArrayList<>();
        int start = 0;
        while (m.find(start)) {
            String g = m.group(1);
            ss.add(g);
            start = m.end();
        }

        return ss;
    }

    public void fpIterable269(final Iterable strings) {
        final List stringList = new ArrayList<>();
        for (final String string : strings) {
            stringList.add(string);
        }
    }

    public List fpGetTransactions247(String xml) {
        List transactions = new ArrayList<>();
        Matcher matcher = REGEX_TXN.matcher(xml.replaceAll("\n", ""));
        while (matcher.find()) {
            transactions.add(matcher.group());
        }
        return transactions;
    }

    public List fpDecodeValue249(BsonReader reader, DecoderContext decoderContext) {
        reader.readStartArray();

        List list = new ArrayList<>();
        while (reader.readBsonType() != BsonType.END_OF_DOCUMENT) {
            list.add(readValue249(reader, decoderContext));
        }
        reader.readEndArray();

        return list;
    }

    public Object readValue249(BsonReader br, DecoderContext dc) {
        return null;
    }

    enum BsonType {
        END_OF_DOCUMENT
    }

    interface BsonReader {
        void readStartArray();

        BsonType readBsonType();

        Object readEndArray();
    }

    interface DecoderContext {

    }

    interface BooReader {
        enum BooTokenType {
            START, MIDDLE, END
        };

        BooTokenType tokenType();

        String nextToken();
    }
}