/*
 * Copyright (C) 2004 Felipe Gustavo de Almeida
 * Copyright (C) 2010-2014 The MPDroid Project
 *
 * All Rights Reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * 1. Redistributions of source code must retain the above copyright
 *    notice,this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

package org.a0z.mpd;

import org.a0z.mpd.exception.InvalidResponseException;

import java.util.ArrayList;
import java.util.Collection;

import static org.a0z.mpd.Tools.KEY;
import static org.a0z.mpd.Tools.VALUE;

/*
 * Class representing one configured output
 */
public class MPDOutput {

    static final String CMD_ID = "outputid";

    private static final String CMD_ENABLED = "outputenabled";

    private static final String CMD_NAME = "outputname";

    private static final String TAG = "MPDOutput";

    private final boolean mEnabled;

    private final int mId;

    private final String mName;

    MPDOutput(final String name, final int id, final boolean enabled) {
        super();

        mName = name;
        mId = id;
        mEnabled = enabled;
    }

    public static MPDOutput build(final Collection<String> response) {
        String name = null;
        int id = -1;
        Boolean enabled = null;

        for (final String[] pair : Tools.splitResponse(response)) {
            switch (pair[KEY]) {
                case CMD_ENABLED:
                    enabled = Boolean.valueOf("1".equals(pair[VALUE]));
                    break;
                case CMD_ID:
                    id = Integer.parseInt(pair[VALUE]);
                    break;
                case CMD_NAME:
                    name = pair[VALUE];
                    break;
                default:
                    Log.warning(TAG,
                            "Non-standard line appeared in output response. Key: " + pair[KEY]
                                    + " value: " + pair[VALUE]);
                    break;
            }
        }

        if (name == null || id == -1 || enabled == null) {
            throw new InvalidResponseException("Failed to parse output information.");
        }

        return new MPDOutput(name, id, enabled.booleanValue());
    }

    public static Collection<MPDOutput> buildOutputsFromList(final Iterable<String> response) {
        final Collection<MPDOutput> result = new ArrayList<>();
        final Collection<String> lineCache = new ArrayList<>(3);

        for (final String line : response) {
            if (line.startsWith(CMD_ID)) {
                if (!lineCache.isEmpty()) {
                    result.add(build(lineCache));
                    lineCache.clear();
                }
            }
            lineCache.add(line);
        }

        if (!lineCache.isEmpty()) {
            result.add(build(lineCache));
        }

        return result;
    }

    public int getId() {
        return mId;
    }

    public String getName() {
        return mName;
    }

    public boolean isEnabled() {
        return mEnabled;
    }

    public String toString() {
        return mName;
    }

}

@@ -1536,24 +1536,47 @@
 .Collection;
+%0Aimport java.util.List;
 %0A%0Aimport sta
@@ -3372,16 +3372,12 @@
 nal 
-Iterable
+List
 %3CStr
@@ -3422,44 +3422,56 @@
 ion%3C
-MPDOutput%3E result = new ArrayList%3C%3E(
+int%5B%5D%3E ranges = Tools.getRanges(response, CMD_ID
 );%0A 
@@ -3498,25 +3498,25 @@
 ion%3C
-String%3E lineCache
+MPDOutput%3E result
  = n
@@ -3534,9 +3534,21 @@
 t%3C%3E(
-3
+ranges.size()
 );%0A%0A
@@ -3570,117 +3570,28 @@
 nal 
-String line : response) %7B%0A            if (line.startsWith(CMD_ID)) %7B%0A                if (!lineCache.isEmpty()
+int%5B%5D range : ranges
 ) %7B%0A
@@ -3598,32 +3598,24 @@
             
-   
-     
 result.add(b
@@ -3623,210 +3623,44 @@
 ild(
-lineCache));%0A                    lineCache.clear();%0A                %7D%0A            %7D%0A            lineCache.add(line);%0A        %7D%0A%0A        if (!lineCache.isEmpty()) %7B%0A            result.add(build(lineCache
+response.subList(range%5B0%5D, range%5B1%5D)
 ));%0A

Use getRanges() with buildOutputsFromList().