location.java

来自「这是一个手机上的J2ME程序」· Java 代码 · 共 155 行

JAVA
155
字号
// J2ME Compass
// Copyright (C) 2007 Dana Peters
// http://www.qcontinuum.org/compass

package org.qcontinuum.compass;

import java.io.*;
import java.util.*;
import org.qcontinuum.astro.*;
import javax.microedition.rms.*;

public class Location {
    
    public String Name;
    public String AirportCode;
    public short LatMinutes;
    public short LongMinutes;
    public TimeZone timeZone;
    public boolean dstEnabled;

    public Location() {
        dstEnabled = true;
    }

    public Location(Location location) {
        Name = location.Name;
        AirportCode = location.AirportCode;
        LatMinutes = location.LatMinutes;
        LongMinutes = location.LongMinutes;
        if (location.timeZone == null)
            timeZone = null;
        else
            timeZone = new TimeZone(location.timeZone);
        dstEnabled = location.dstEnabled;
    }
    
    public Location(String name) {
        Name = name;
        load();
    }

    public Location(DataInputStream inputStream) throws IOException {
        load(inputStream);
    }
    
    public Location(String name, int latMinutes, int longMinutes) {
        Name = name;
        AirportCode = null;
        LatMinutes = (short)latMinutes;
        LongMinutes = (short)longMinutes;
        timeZone = null;
        dstEnabled = false;
    }
    
    public EarthPosition getEarthPosition() {
        return new EarthPosition(LatMinutes, LongMinutes);
    }

    public Date getGmt(Date localDate) {
        return timeZone.getGmt(localDate, dstEnabled);
    }
    
    public Date getLocalTime(Date gmtDate) {
        return timeZone.getLocalTime(gmtDate, dstEnabled);
    }
    
    public int getStdOffset() {
        return timeZone.getStdOffset();
    }
    
    public int getDstOffset(Date localDate) {
        return timeZone.getDstOffset(localDate, dstEnabled);
    }

    public void load(String name) {
        Name = name;
        load();
    }
 
    private void load() {
        RecordStore recordStore;
        try {
	    recordStore = RecordStore.openRecordStore(Name, false);
            byte[] bytes = recordStore.getRecord(1);
            ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
            DataInputStream inputStream = new DataInputStream(bais);
            load(inputStream);
            inputStream.close();
            recordStore.closeRecordStore();
        }
        catch (Exception exception) {
            Name = AirportCode = null;
            LatMinutes = LongMinutes = 0;
            timeZone = new TimeZone();
            return;
        }
    }
    
    public void load(DataInputStream inputStream) throws IOException {
        Name = restoreNull(inputStream.readUTF());
        AirportCode = restoreNull(inputStream.readUTF());
        LatMinutes = inputStream.readShort();
        LongMinutes = inputStream.readShort();
        if (inputStream.readBoolean())
            timeZone = new TimeZone(inputStream);
        else
            timeZone = null;
        dstEnabled = inputStream.readBoolean();
    }

    public void save() {
        RecordStore recordStore;
        try {
            recordStore = RecordStore.openRecordStore(Name, true);
        }
        catch (RecordStoreException rsc) {
            return;
        }
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        DataOutputStream outputStream = new DataOutputStream(baos);
        try {
            save(outputStream);
            byte[] bytes = baos.toByteArray();
            if (recordStore.getNumRecords() == 0)
                recordStore.addRecord(bytes, 0, bytes.length);
            else
                recordStore.setRecord(1, bytes, 0, bytes.length);
            outputStream.close();
            recordStore.closeRecordStore();
        }
        catch (Exception exception) {
        }
    }

    public void save(DataOutputStream outputStream) throws IOException {
        outputStream.writeUTF(removeNull(Name));
        outputStream.writeUTF(removeNull(AirportCode));
        outputStream.writeShort(LatMinutes);
        outputStream.writeShort(LongMinutes);
        outputStream.writeBoolean(timeZone != null);
        if (timeZone != null)
            timeZone.save(outputStream);
        outputStream.writeBoolean(dstEnabled);
    }
    
    private String removeNull(String text) {
        return text != null ? text : "";
    }
    
    private String restoreNull(String text) {
        return text.length() > 0 ? text : null;
    }

}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?