Code example for WeakReference

Methods: get

0
     */ 
    public SimpleHogBug[] getBugReport() { 
        if (bugData == null || bugData.get() == null) {
            readBugReport(); 
        } 
        if (bugData == null || bugData.get() == null)
            return null; 
        return bugData.get();
    } 
 
    /** 
     * @return the hog reports 
     */ 
    public SimpleHogBug[] getHogReport() { 
        if (hogData == null || hogData.get() == null) {
            readHogReport(); 
        } 
        if (hogData == null || hogData.get() == null)
            return null; 
        return hogData.get();
    } 
 
    public void writeBugReport(HogBugReport r) {
        if (r != null) {
            SimpleHogBug[] list = convert(r.getHbList(), true);
            if (list != null){
                bugData = new WeakReference<SimpleHogBug[]>(list);
                writeObject(list, BUGFILE);
            } 
        } 
    } 
 
    public void writeHogReport(HogBugReport r) {
        if (r != null) {
            SimpleHogBug[] list = convert(r.getHbList(), false);
            if (list != null){
                hogData = new WeakReference<SimpleHogBug[]>(list);
                writeObject(list, HOGFILE);
            } 
        } 
    } 
 
    private SimpleHogBug[] convert(List<HogsBugs> list, boolean isBug) {
        if (list == null)
            return null; 
        SimpleHogBug[] result = new SimpleHogBug[list.size()];
        int size = list.size();
        for (int i = 0; i < size; ++i) {
            HogsBugs item = list.get(i);
            result[i] = new SimpleHogBug(fixName(item.getAppName()), isBug ? Type.BUG:Type.HOG);
            result[i].setAppLabel(item.getAppLabel());
            String priority = item.getAppPriority();
            if (priority == null || priority.length() == 0)
                priority = "Foreground app";
            result[i].setAppPriority(priority);
            result[i].setExpectedValue(item.getExpectedValue());
            result[i].setExpectedValueWithout(item.getExpectedValueWithout());
            result[i].setwDistance(item.getWDistance());
            //result[i].setxVals(convert(item.getXVals())); 
            result[i].setError(item.getError());
            result[i].setErrorWithout(item.getErrorWithout());
            result[i].setSamples(item.getSamples());
            result[i].setSamplesWithout(item.getSamplesWithout());
            //result[i].setSignificance(item.getSignificance()); 
            /*result[i].setyVals(convert(item.getYVals())); 
            result[i].setxValsWithout(convert(item.getXValsWithout())); 
            result[i].setyValsWithout(convert(item.getYValsWithout()));*/ 
        } 
        return result;
    } 
 
    public static double[] convert(List<Double> dbls) {
        if (dbls == null)
            return new double[0]; 
        for (int j = 0; j < dbls.size(); ++j) {
            if (dbls.get(j) == 0.0) {
                dbls.remove(j);
                j--;
            } 
        } 
        double[] arr = new double[dbls.size()];
        for (int j = 0; j < dbls.size(); ++j) {
            arr[j] = dbls.get(j);
        } 
        return arr;
    } 
 
    private String fixName(String name) {
        if (name == null)
            return null; 
        int idx = name.lastIndexOf(':');
        if (idx <= 0)
            idx = name.length();
        String n = name.substring(0, idx);
        return n;
    } 
 
    public SimpleHogBug[] readBugReport() { 
        Object o = readObject(BUGFILE);
        //Log.d("CaratDataStorage", "Read Bugs: " + o); 
        if (o == null || !(o instanceof SimpleHogBug[]))
            return null; 
        SimpleHogBug[] r = (SimpleHogBug[]) o;
        bugData = new WeakReference<SimpleHogBug[]>(r);
        return r;
    } 
 
    public SimpleHogBug[] readHogReport() { 
        Object o = readObject(HOGFILE);