Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
/*
 ** Licensed under the Apache License, Version 2.0 (the "License");
 ** you may not use this file except in compliance with the License.
 ** You may obtain a copy of the License at
 **
 **     http://www.apache.org/licenses/LICENSE-2.0
 **
 ** Unless required by applicable law or agreed to in writing, software
 ** distributed under the License is distributed on an "AS IS" BASIS,
 ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 ** See the License for the specific language governing permissions and
 ** limitations under the License.
 */

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Main {
    private static final Pattern PAT_ATLASQUEST = Pattern.compile(".*\\((.*)#(.*)\\)");
    private static final Pattern PAT_ATSIGN_FORMAT = Pattern.compile("([^@]*)@(.*)");
    private static final Pattern PAT_LATLON = Pattern.compile("([NS]?[^NSEW,]*[NS]?),?\\s*([EW]?.*)");
    private static final Pattern PAT_LATLONDEC = Pattern.compile("([\\d.]+)\\s+([\\d.]+)");
    private static final Pattern PAT_PAREN_FORMAT = Pattern.compile("([^(]*)\\(([^)]*).*");

    public static CharSequence[] splitLatLonDescription(CharSequence location) {
        CharSequence coordsAndDescription[] = splitCoordsAndDescription(location);
        CharSequence latLon[] = splitLatLon(coordsAndDescription[0]);

        CharSequence[] parseDescription = parseDescription(coordsAndDescription[1]);
        return new CharSequence[] { latLon[0], latLon[1], parseDescription[0], parseDescription[1] };
    }

    public static CharSequence[] splitCoordsAndDescription(CharSequence location) {
        Matcher matcher = PAT_ATSIGN_FORMAT.matcher(location);
        if (matcher.matches()) {
            return new CharSequence[] { matcher.group(2).trim(), matcher.group(1).trim() };
        }
        matcher = PAT_PAREN_FORMAT.matcher(location);
        if (matcher.matches()) {
            return new CharSequence[] { matcher.group(1).trim(), matcher.group(2).trim() };
        }
        // No description.
        return new CharSequence[] { location, "" };
    }

    public static CharSequence[] splitLatLon(CharSequence string) {
        Matcher matcherDecimal = PAT_LATLONDEC.matcher(string);
        if (matcherDecimal.matches()) {
            return new String[] { matcherDecimal.group(1).trim(), matcherDecimal.group(2).trim() };
        }
        Matcher matcher = PAT_LATLON.matcher(string);
        matcher.matches();
        return new String[] { matcher.group(1).trim(), matcher.group(2).trim() };
    }

    public static CharSequence[] parseDescription(CharSequence string) {
        Matcher matcher = PAT_ATLASQUEST.matcher(string);
        if (matcher.matches())
            return new CharSequence[] { "LB" + matcher.group(2).trim(), matcher.group(1).trim() };
        return new CharSequence[] { string, "" };
    }
}