Example usage for android.support.v4.util CircularArray CircularArray

List of usage examples for android.support.v4.util CircularArray CircularArray

Introduction

In this page you can find the example usage for android.support.v4.util CircularArray CircularArray.

Prototype

public CircularArray() 

Source Link

Usage

From source file:Main.java

@SafeVarargs
public static <T> CircularArray<T> asArray(T... items) {
    CircularArray<T> result = new CircularArray<>();
    if (items != null) {
        for (T t : items) {
            result.addLast(t);//from w  w w  .  ja va2 s  .com
        }
    }
    return result;
}

From source file:com.appsimobile.appsii.module.weather.loader.WeatherDataParserTest.java

public void testWeatherParser_multipleResults() throws ParseException, ResponseParserException, JSONException {
    AssetManager manager = getContext().getAssets();
    String json = AssetUtils.readAssetToString(manager, "weather.json", new StringBuilder());
    CircularArray<WeatherData> result = new CircularArray<>();
    WeatherDataParser.parseWeatherData(result, json, ArrayUtils.asArray("1", "2", "3"));

    assertEquals(3, result.size());/*from  ww w . j av a 2 s  . c o  m*/

    {
        WeatherData data1 = result.get(0);

        assertEquals("Breda", data1.location);
        assertEquals(34, data1.windChill);
        assertEquals(80, data1.windDirection);
        assertEquals(3.0f, data1.windSpeed);

        assertEquals(81, data1.atmosphereHumidity);
        assertEquals(29.85f, data1.atmospherePressure);
        assertEquals(0, data1.atmosphereRising);
        assertEquals(6.21f, data1.atmosphereVisible);

        assertEquals("8:34 am", data1.sunrise);
        assertEquals("5:11 pm", data1.sunset);

        assertEquals(26, data1.nowConditionCode);
        assertEquals(34, data1.nowTemperature);
        assertEquals("Cloudy", data1.nowConditionText);

        assertEquals("1", data1.woeid);

        assertEquals(5, data1.forecasts.size());
        {
            WeatherData.Forecast fc = data1.forecasts.get(0);
            assertEquals(2457044, fc.julianDay);
            assertEquals(36, fc.high);
            assertEquals(28, fc.low);
            assertEquals(29, fc.conditionCode);
            assertEquals("Partly Cloudy", fc.forecastText);
        }
    }

}

From source file:com.appsimobile.appsii.module.weather.loader.YahooWeatherApiClient.java

@Nullable
public static CircularArray<WeatherData> getWeatherForWoeids(CircularArray<String> woeids, String unit)
        throws CantGetWeatherException {

    if (woeids == null || woeids.isEmpty())
        return null;

    HttpURLConnection connection = null;
    try {/*from w w w .ja v  a  2 s. c  o  m*/
        String url = buildWeatherQueryUrl(woeids, unit);
        if (BuildConfig.DEBUG)
            Log.d("YahooWeatherApiClient", "loading weather from: " + url);

        connection = Utils.openUrlConnection(url);

        CircularArray<WeatherData> result = new CircularArray<>();
        WeatherDataParser.parseWeatherData(result, connection.getInputStream(), woeids);

        return result;
    } catch (JSONException | ResponseParserException | ParseException | IOException | NumberFormatException e) {
        throw new CantGetWeatherException(true, R.string.no_weather_data, "Error parsing weather feed XML.", e);
    } finally {
        if (connection != null) {
            connection.disconnect();
        }
    }
}

From source file:com.appsimobile.appsii.module.weather.loader.WeatherDataParserTest.java

public void testWeatherParser_singleResult() throws ParseException, ResponseParserException, JSONException {
    AssetManager manager = getContext().getAssets();
    String json = AssetUtils.readAssetToString(manager, "weather1.json", new StringBuilder());
    CircularArray<WeatherData> result = new CircularArray<>();
    WeatherDataParser.parseWeatherData(result, json, ArrayUtils.asArray("1"));

    assertEquals(1, result.size());// w  w  w  .  j a  va2 s . c  o m

    {
        WeatherData data1 = result.get(0);

        assertEquals("Etten-Leur", data1.location);
        assertEquals(-6, data1.windChill);
        assertEquals(70, data1.windDirection);
        assertEquals(14.48f, data1.windSpeed);

        assertEquals(83, data1.atmosphereHumidity);
        assertEquals(1015.92f, data1.atmospherePressure);
        assertEquals(0, data1.atmosphereRising);
        assertEquals(5f, data1.atmosphereVisible);

        assertEquals("8:32 am", data1.sunrise);
        assertEquals("5:13 pm", data1.sunset);

        assertEquals(26, data1.nowConditionCode);
        assertEquals(-1, data1.nowTemperature);
        assertEquals("Cloudy", data1.nowConditionText);

        assertEquals("1", data1.woeid);

        assertEquals(5, data1.forecasts.size());
        {
            WeatherData.Forecast fc = data1.forecasts.get(0);
            assertEquals(2457045, fc.julianDay);
            assertEquals(1, fc.high);
            assertEquals(-2, fc.low);
            assertEquals(29, fc.conditionCode);
            assertEquals("Partly Cloudy", fc.forecastText);
        }
    }

}

From source file:com.appsimobile.appsii.module.weather.loader.WeatherDataParserTest.java

public void testWeatherParserWithError() throws ParseException, ResponseParserException, JSONException {
    AssetManager manager = getContext().getAssets();
    String json = AssetUtils.readAssetToString(manager, "weather_with_error.json", new StringBuilder());
    CircularArray<WeatherData> result = new CircularArray<>();
    WeatherDataParser.parseWeatherData(result, json, ArrayUtils.asArray("a", "b"));

    assertEquals(1, result.size());/*w w w.  j  a v  a  2 s  .  com*/

    {
        WeatherData data1 = result.get(0);

        assertEquals("Etten-Leur", data1.location);
        assertEquals(24, data1.windChill);
        assertEquals(60, data1.windDirection);
        assertEquals(6.0f, data1.windSpeed);

        assertEquals(91, data1.atmosphereHumidity);
        assertEquals(29.85f, data1.atmospherePressure);
        assertEquals(0, data1.atmosphereRising);
        assertEquals(3.73f, data1.atmosphereVisible);

        assertEquals("8:34 am", data1.sunrise);
        assertEquals("5:11 pm", data1.sunset);

        assertEquals(26, data1.nowConditionCode);
        assertEquals(30, data1.nowTemperature);
        assertEquals("Cloudy", data1.nowConditionText);

        assertEquals("b", data1.woeid);
        assertEquals(5, data1.forecasts.size());
        {
            WeatherData.Forecast fc = data1.forecasts.get(0);
            assertEquals(2457044, fc.julianDay);
            assertEquals(35, fc.high);
            assertEquals(27, fc.low);
            assertEquals(29, fc.conditionCode);
            assertEquals("Partly Cloudy", fc.forecastText);
        }
    }

}

From source file:com.appsimobile.appsii.module.weather.loader.YahooWeatherApiClient.java

@VisibleForTesting
static LocationInfo parseLocationInfo(LocationInfo li, InputStream in)
        throws XmlPullParserException, IOException, CantGetWeatherException {

    CircularArray<Pair<String, String>> alternateWoeids = new CircularArray<>();
    String primaryWoeid = null;//from   w w  w  . j  a v a2 s  .c o m

    XmlPullParser xpp = sXmlPullParserFactory.newPullParser();
    xpp.setInput(new InputStreamReader(in));

    boolean inWoe = false;
    boolean inTown = false;
    boolean inCountry = false;
    boolean inTimezone = false;
    int eventType = xpp.getEventType();

    while (eventType != XmlPullParser.END_DOCUMENT) {
        String tagName = xpp.getName();

        if (eventType == XmlPullParser.START_TAG && "woeid".equals(tagName)) {
            inWoe = true;
        } else if (eventType == XmlPullParser.TEXT && inWoe) {
            primaryWoeid = xpp.getText();
        } else if (eventType == XmlPullParser.START_TAG && tagName.startsWith("timezone")) {
            inTimezone = true;
        } else if (eventType == XmlPullParser.TEXT && inTimezone) {
            li.timezone = xpp.getText();
        } else if (eventType == XmlPullParser.START_TAG && (tagName.startsWith("locality")
                || tagName.startsWith("admin") || tagName.startsWith("country"))) {
            for (int i = xpp.getAttributeCount() - 1; i >= 0; i--) {
                String attrName = xpp.getAttributeName(i);
                if ("type".equals(attrName) && "Town".equals(xpp.getAttributeValue(i))) {
                    inTown = true;
                } else if ("type".equals(attrName) && "Country".equals(xpp.getAttributeValue(i))) {
                    inCountry = true;
                } else if ("woeid".equals(attrName)) {
                    String woeid = xpp.getAttributeValue(i);
                    if (!TextUtils.isEmpty(woeid)) {
                        alternateWoeids.addLast(new Pair<>(tagName, woeid));
                    }
                }
            }
        } else if (eventType == XmlPullParser.TEXT && inTown) {
            li.town = xpp.getText();
        } else if (eventType == XmlPullParser.TEXT && inCountry) {
            li.country = xpp.getText();
        }

        if (eventType == XmlPullParser.END_TAG) {
            inWoe = false;
            inTown = false;
            inCountry = false;
            inTimezone = false;
        }

        eventType = xpp.next();
    }

    // Add the primary woeid if it was found.
    if (!TextUtils.isEmpty(primaryWoeid)) {
        li.woeids.addLast(primaryWoeid);
    }

    // Sort by descending tag name to order by decreasing precision
    // (locality3, locality2, locality1, admin3, admin2, admin1, etc.)
    ArrayUtils.sort(alternateWoeids, new Comparator<Pair<String, String>>() {
        @Override
        public int compare(Pair<String, String> pair1, Pair<String, String> pair2) {
            return pair1.first.compareTo(pair2.first);
        }
    });

    int N = alternateWoeids.size();
    for (int i = 0; i < N; i++) {
        Pair<String, String> pair = alternateWoeids.get(i);
        li.woeids.addLast(pair.second);
    }

    if (li.woeids.size() > 0) {
        return li;
    }

    throw new CantGetWeatherException(true, R.string.no_weather_data, "No WOEIDs found nearby.");
}

From source file:com.appsimobile.appsii.module.weather.loader.YahooWeatherApiClient.java

public static CircularArray<LocationSearchResult> findLocationsAutocomplete(String startsWith) {
    CircularArray<LocationSearchResult> results = new CircularArray<>();

    HttpURLConnection connection = null;
    try {//www  .j  a va 2 s . c  o  m
        connection = Utils.openUrlConnection(buildPlaceSearchStartsWithUrl(startsWith));
        InputStream inputStream = connection.getInputStream();

        parseLocationSearchResults(results, inputStream);

    } catch (IOException | XmlPullParserException e) {
        Log.w(TAG, "Error parsing place search XML");
    } finally {
        if (connection != null) {
            connection.disconnect();
        }
    }

    return results;
}