/*
 * 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.item;

import java.text.Collator;
import java.util.Collections;
import java.util.List;
import java.util.Locale;

public abstract class Item implements Comparable<Item> {

    public static final Collator defaultCollator = Collator.getInstance(Locale.getDefault());

    /*
     * Merge item lists, for example received by albumartist and artist
     * requests. Sorted lists required!
     */
    public static <T extends Item> List<T> merged(List<T> aa_items,
            List<T> a_items) {
        int j_start = aa_items.size() - 1;
        for (int i = a_items.size() - 1; i >= 0; i--) { // artists
            for (int j = j_start; j >= 0; j--) { // album artists
                if (aa_items.get(j).nameEquals(a_items.get(i))) {
                    j_start = j;
                    a_items.remove(i);
                    break;
                }
            }
        }
        a_items.addAll(aa_items);
        Collections.sort(a_items);
        return a_items;
    }

    @Override
    public int compareTo(Item o) {
        // sort "" behind everything else
        if (sortText() != null && sortText().isEmpty()) {
            if (o.sortText() != null && o.sortText().isEmpty()) {
                return 0;
            }
            return 1;
        }
        if (o.sortText() != null && o.sortText().isEmpty()) {
            return -1;
        }
        return defaultCollator.compare(sortText(), o.sortText());
        // return sort().compareToIgnoreCase(o.sort());
    }

    public abstract String getName();

    public String info() {
        return toString();
    }

    public boolean isUnknown() {
        return getName().isEmpty();
    }

    public String mainText() {
        return getName();
    }

    public boolean nameEquals(Item o) {
        return getName().equals(o.getName());
    }

    public String sortText() {
        return getName().toLowerCase(Locale.getDefault());
    }

    @Override
    public String toString() {
        return mainText();
    }

}

@@ -1630,31 +1630,32 @@
 ollator 
-defaultCollator
+DEFAULT_COLLATOR
  = Colla
@@ -1754,16 +1754,17 @@
 by album
+ 
 artist a
@@ -1871,16 +1871,22 @@
  merged(
+final 
 List%3CT%3E 
@@ -1886,22 +1886,26 @@
 ist%3CT%3E a
-a_item
+lbumArtist
 s,%0A     
@@ -1911,16 +1911,22 @@
         
+final 
 List%3CT%3E 
@@ -1926,21 +1926,21 @@
 ist%3CT%3E a
-_item
+rtist
 s) %7B%0A   
@@ -1959,22 +1959,26 @@
 tart = a
-a_item
+lbumArtist
 s.size()
@@ -2005,21 +2005,21 @@
 nt i = a
-_item
+rtist
 s.size()
@@ -2137,22 +2137,26 @@
    if (a
-a_item
+lbumArtist
 s.get(j)
@@ -2168,21 +2168,21 @@
 Equals(a
-_item
+rtist
 s.get(i)
@@ -2240,21 +2240,21 @@
        a
-_item
+rtist
 s.remove
@@ -2336,21 +2336,21 @@
        a
-_item
+rtist
 s.addAll
@@ -2351,22 +2351,26 @@
 addAll(a
-a_item
+lbumArtist
 s);%0A    
@@ -2391,21 +2391,21 @@
 s.sort(a
-_item
+rtist
 s);%0A    
@@ -2420,13 +2420,13 @@
 rn a
-_item
+rtist
 s;%0A 
@@ -2470,25 +2470,74 @@
 mpareTo(
-Item o) %7B
+final Item another) %7B%0A        final int comparisonResult;%0A
 %0A       
@@ -2637,33 +2637,39 @@
             if (
-o
+another
 .sortText() != n
@@ -2667,33 +2667,39 @@
 xt() != null && 
-o
+another
 .sortText().isEm
@@ -2723,22 +2723,34 @@
         
-return
+comparisonResult =
  0;%0A    
@@ -2750,32 +2750,39 @@
 0;%0A            %7D
+ else %7B
 %0A            ret
@@ -2778,22 +2778,38 @@
         
-return
+    comparisonResult =
  1;%0A    
@@ -2804,32 +2804,36 @@
 lt = 1;%0A        
+    
 %7D%0A        if (o.
@@ -2830,13 +2830,26 @@
     
-if (o
+%7D else if (another
 .sor
@@ -2867,17 +2867,23 @@
 null && 
-o
+another
 .sortTex
@@ -2911,22 +2911,34 @@
         
-return
+comparisonResult =
  -1;%0A   
@@ -2935,32 +2935,39 @@
  = -1;%0A        %7D
+ else %7B
 %0A        return 
@@ -2963,30 +2963,47 @@
     
-return defaultCollator
+    comparisonResult = DEFAULT_COLLATOR
 .com
@@ -3019,17 +3019,23 @@
 Text(), 
-o
+another
 .sortTex
@@ -3052,54 +3052,42 @@
     
-// return sort().compareToIgnoreCase(o.sort())
+%7D%0A%0A        return comparisonResult
 ;%0A  
@@ -3361,16 +3361,22 @@
 eEquals(
+final 
 Item o) 

Clean up warnings