1
0
Fork 0
mirror of https://bitbucket.org/akapribot/owm-japis.git synced 2025-04-28 08:28:12 -07:00

2.5.0.1 is ready!

This commit is contained in:
Ashutosh Kumar Singh 2013-08-10 23:52:09 +05:30
parent 391485a605
commit de1786a2d9
118 changed files with 32045 additions and 0 deletions

View file

@ -0,0 +1,209 @@
/*
* Copyright (C)2013 Ashutosh Kumar Singh [me@AKSingh.net]
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* The Software shall be used for Good, not Evil.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
* OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*/
package net.aksingh.java.api.owm;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Scanner;
/**
* Provides various tools, which help doing tasks in this application.
* For example, tool for downloading content from the Internet, tool for
* correcting and stricting web addresses, etc.
*
* <p>Note: This class directly do not provide any functions, but has
* <code>static</code> sub-classes which provide various
* related tools, i.e., this class only behaves as the container for those
* classes.
*
* @author Ashutosh Kumar Singh
* @version 2013-08-05
* @since 2.5.0.1
*/
public class Tools {
/**
* Provides methods to download data or files from the Internet.
*
* @author Ashutosh Kumar Singh
* @version 2013-07-24
* @since 2.5.0.1
*/
public static class Downloader {
/**
* Downloads a page/content from the Internet.
* This method gets the content of the web page, whose URL is given by
* the <code>pageAddress</code>.
*
* <p>NOTE: <code>pageAddress</code> should be a correct URL, else
* this method will throw {@link MalformedURLException}.
*
* @param pageAddress
* Address of the web page to get from the Internet.
*
* @return Content of the web page
*
* @throws MalformedURLException
* Address of the web page is not correct.
*
* @throws IOException
* Error while loading the page from the Internet or connection
* got disconnected.
*/
public static String downloadPage(String pageAddress)
throws MalformedURLException, IOException {
String webPage = null;
URL url = new URL(pageAddress);
InputStream iStream = url.openStream();
if (iStream != null) {
// converting InputStream to String (as we require String)
webPage = (new Scanner(iStream)).useDelimiter("\\A").next();
}
iStream.close();
return webPage;
}
}
/**
* Provides functions to handle HTML, e.g., correcting
* non-compliant or non-strict web addresses, etc.
*
* @author Ashutosh Kumar Singh
* @version 2013/08/05
* @since 2.5.0.1
*/
public static class HTML {
/**
* Stricts a non-strict or non-compliant web address
* to a proper URL.
*
* <p>Note: This function checks and stricts only some parts of the
* URL, which are necessary to be corrected for accessing data from
* the OpenWeatherMap.org
*
* @param inputString String to be converted to strict HTML address
* @return HTML standards-followed and stricted URL
*/
public static String strictAddress(String inputString) {
String outputString = "";
if (inputString != null) {
outputString = inputString.replace(" ", "%20").replace(".", "%2E");
}
return outputString;
}
}
/**
* Provides methods to do conversions.
* For example, converting degree to direction, etc.
*
* @author Ashutosh Kumar Singh
* @version 2013/08/05
* @since 2.5.0.1
*/
public static class Convertor {
/**
* Converts degree to direction.
* @return Direction
*
* @throws IllegalArgumentException
* Degree should be between 0 and 360.
*/
public String convertDegree2Direction(float degree)
throws IllegalArgumentException{
String direction;
// degree should be between 0 and 360
if ((degree < 0.0f) || (degree > 360.0f)) {
throw new IllegalArgumentException("Degree cannot be less than 0 or more than 360.");
}
if (degree <= 11.25f) {
direction = "N";
}
else if (degree <= 33.75f) {
direction = "NNE";
}
else if (degree <= 56.25f) {
direction = "NE";
}
else if (degree <= 78.75f) {
direction = "ENE";
}
else if (degree <= 101.25f) {
direction = "E";
}
else if (degree <= 123.75f) {
direction = "ESE";
}
else if (degree <= 146.25f) {
direction = "SE";
}
else if (degree <= 168.75f) {
direction = "SSE";
}
else if (degree <= 191.25f) {
direction = "S";
}
else if (degree <= 213.75f) {
direction = "SSW";
}
else if (degree <= 236.25f) {
direction = "SW";
}
else if (degree <= 258.75f) {
direction = "WSW";
}
else if (degree <= 281.25f) {
direction = "W";
}
else if (degree <= 303.75f) {
direction = "WNW";
}
else if (degree <= 326.25f) {
direction = "NW";
}
else if (degree <= 348.75f) {
direction = "NNW";
}
else {
direction = "N";
}
return direction;
}
}
}