views:

105

answers:

1

For some reason when I deserialize my quotes ArrayList, I don't get the right object back. I want to make sure that whenever I read/write my object, it will always be the same object.

Serialization code:

private void serializeQuotes(){
        FileOutputStream fos;
        try {
            fos = openFileOutput(Constants.FILENAME, Context.MODE_PRIVATE);
            ObjectOutputStream oos = new ObjectOutputStream(fos);
            oos.writeObject(quotes); 
            oos.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }catch(IOException e){
            e.printStackTrace();
        }
    }

    @SuppressWarnings("unchecked")
    private void deserializeQuotes(){
        try{
            FileInputStream fis = openFileInput(Constants.FILENAME);
            ObjectInputStream ois = new ObjectInputStream(fis);
            quotes = (ArrayList<Quote>) ois.readObject();
            fis.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }catch(IOException e){
            e.printStackTrace();
        }catch(ClassNotFoundException e){
            e.printStackTrace();
        }
    }

Here is my Quote object

package org.stocktwits.model;

import java.io.Serializable;

public class Quote implements Serializable {

    private static final long serialVersionUID = 1L;

    public String symbol;
    public String name;
    public String change;
    public String percentChange;
    public String open;
    public String daysHigh;
    public String daysLow;
    public String volume;
    public String peRatio;
    public String marketCapitalization;
    public String yearHigh;
    public String yearLow;
    public String lastTradePriceOnly;

    public String getSymbol() {
        return symbol;
    }
    public void setSymbol(String symbol) {
        this.symbol = symbol;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getChange() {
        return change;
    }
    public void setChange(String change) {
        this.change = change;
    }
    public String getPercentChange() {
        return percentChange;
    }
    public void setPercentChange(String percentChange) {
        this.percentChange = percentChange;
    }
    public String getOpen() {
        return open;
    }
    public void setOpen(String open) {
        this.open = open;
    }
    public String getDaysHigh() {
        return daysHigh;
    }
    public void setDaysHigh(String daysHigh) {
        this.daysHigh = daysHigh;
    }
    public String getDaysLow() {
        return daysLow;
    }
    public void setDaysLow(String daysLow) {
        this.daysLow = daysLow;
    }
    public String getVolume() {
        return volume;
    }
    public void setVolume(String volume) {
        this.volume = volume;
    }
    public String getPeRatio() {
        return peRatio;
    }
    public void setPeRatio(String peRatio) {
        this.peRatio = peRatio;
    }
    public String getMarketCapitalization() {
        return marketCapitalization;
    }
    public void setMarketCapitilization(String marketCapitalization) {
        this.marketCapitalization = marketCapitalization;
    }
    public String getYearHigh() {
        return yearHigh;
    }
    public void setYearHigh(String yearHigh) {
        this.yearHigh = yearHigh;
    }
    public String getYearLow() {
        return yearLow;
    }
    public void setYearLow(String yearLow) {
        this.yearLow = yearLow;
    }

    public String getLastTradePriceOnly() {
        return lastTradePriceOnly;
    }

    public void setLastTradePriceOnly(String lastTradePriceOnly) {
        this.lastTradePriceOnly = lastTradePriceOnly;
    }

    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + ((change == null) ? 0 : change.hashCode());
        result = prime * result
                + ((daysHigh == null) ? 0 : daysHigh.hashCode());
        result = prime * result + ((daysLow == null) ? 0 : daysLow.hashCode());
        result = prime
                * result
                + ((lastTradePriceOnly == null) ? 0 : lastTradePriceOnly
                        .hashCode());
        result = prime
                * result
                + ((marketCapitalization == null) ? 0 : marketCapitalization
                        .hashCode());
        result = prime * result + ((name == null) ? 0 : name.hashCode());
        result = prime * result + ((open == null) ? 0 : open.hashCode());
        result = prime * result + ((peRatio == null) ? 0 : peRatio.hashCode());
        result = prime * result
                + ((percentChange == null) ? 0 : percentChange.hashCode());
        result = prime * result + ((symbol == null) ? 0 : symbol.hashCode());
        result = prime * result + ((volume == null) ? 0 : volume.hashCode());
        result = prime * result
                + ((yearHigh == null) ? 0 : yearHigh.hashCode());
        result = prime * result + ((yearLow == null) ? 0 : yearLow.hashCode());
        return result;
    }
    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        Quote other = (Quote) obj;
        if (change == null) {
            if (other.change != null)
                return false;
        } else if (!change.equals(other.change))
            return false;
        if (daysHigh == null) {
            if (other.daysHigh != null)
                return false;
        } else if (!daysHigh.equals(other.daysHigh))
            return false;
        if (daysLow == null) {
            if (other.daysLow != null)
                return false;
        } else if (!daysLow.equals(other.daysLow))
            return false;
        if (lastTradePriceOnly == null) {
            if (other.lastTradePriceOnly != null)
                return false;
        } else if (!lastTradePriceOnly.equals(other.lastTradePriceOnly))
            return false;
        if (marketCapitalization == null) {
            if (other.marketCapitalization != null)
                return false;
        } else if (!marketCapitalization.equals(other.marketCapitalization))
            return false;
        if (name == null) {
            if (other.name != null)
                return false;
        } else if (!name.equals(other.name))
            return false;
        if (open == null) {
            if (other.open != null)
                return false;
        } else if (!open.equals(other.open))
            return false;
        if (peRatio == null) {
            if (other.peRatio != null)
                return false;
        } else if (!peRatio.equals(other.peRatio))
            return false;
        if (percentChange == null) {
            if (other.percentChange != null)
                return false;
        } else if (!percentChange.equals(other.percentChange))
            return false;
        if (symbol == null) {
            if (other.symbol != null)
                return false;
        } else if (!symbol.equals(other.symbol))
            return false;
        if (volume == null) {
            if (other.volume != null)
                return false;
        } else if (!volume.equals(other.volume))
            return false;
        if (yearHigh == null) {
            if (other.yearHigh != null)
                return false;
        } else if (!yearHigh.equals(other.yearHigh))
            return false;
        if (yearLow == null) {
            if (other.yearLow != null)
                return false;
        } else if (!yearLow.equals(other.yearLow))
            return false;
        return true;
    }
}
+1  A: 

Why don't you delete the file in "serializeQuotes()" before writing the object. That way you will be sure, that there will be only one object there.

private void serializeQuotes(){
         FileOutputStream fos;
         File file = new File(Constants.FILENAME);
         if (file.exists()) file.delete();
        try {
            fos = openFileOutput(file, Context.MODE_PRIVATE);
            ObjectOutputStream oos = new ObjectOutputStream(fos);
            oos.writeObject(quotes); 
            oos.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }catch(IOException e){
            e.printStackTrace();
        }
    }

Or if you do not want to delete file every time, use some kind of iteration when reading obejcts from it.

kofucii