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

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

package ex;

import java.io.BufferedReader;
import java.io.IOException;
import java.util.ArrayList;
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 com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.google.common.collect.Sets;

public class PSC_Sample {

    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 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;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy