package org.vaadin.viritin.util;

import java.util.*;
import java.util.Locale.LanguageRange;

import org.vaadin.viritin.util.VaadinLocale.LocaleNegotiationStrategey;

import com.vaadin.server.VaadinRequest;

/**
 * Implementation of {@link LocaleNegotiationStrategey} which uses java 1.8
 * {@link Locale#lookup(List, Collection)} to negotiate the best locale.
 * 
 * @author Daniel Nordhoff-Vergien
 *
 */
public class Java8LocaleNegotiationStrategy implements
        LocaleNegotiationStrategey {

    @Override
    public Locale negotiate(List<Locale> supportedLocales,
            VaadinRequest vaadinRequest) {
        String languages = vaadinRequest.getHeader("Accept-Language");
        List<LanguageRange> priorityList = Locale.LanguageRange
                .parse(languages);
        return Locale.lookup(priorityList, supportedLocales);
    }
}

@@ -43,53 +43,47 @@
 ava.
-util.*;%0Aimport java.util.Locale.LanguageRange
+lang.reflect.Method;%0Aimport java.util.*
 ;%0A%0Ai
@@ -686,50 +686,174 @@
     
-List%3CLanguageRange%3E priorityList = 
+try %7B%0A            // Use reflection here, so the code compiles with jdk 1.7%0A            Class%3C?%3E languageRange = Class%0A                    .forName(%22java.util.
 Locale
-.
+$
 Lang
@@ -861,16 +861,19 @@
 ageRange
+%22);
 %0A       
@@ -881,31 +881,252 @@
     
-    .parse(languages);%0A
+Method parse = languageRange.getMethod(%22parse%22, String.class);%0A            Object priorityList = parse.invoke(null, languages);%0A            Method lookup = Locale.class.getMethod(%22lookup%22, List.class,%0A                    Collection.class);%0A    
     
@@ -1140,22 +1140,37 @@
 urn 
+(
 Locale
-.
+) 
 lookup
-(
+.invoke(null, 
 prio
@@ -1194,20 +1194,206 @@
 edLocales);%0A
+        %7D catch (Exception e) %7B%0A            throw new RuntimeException(%0A                    %22Java8LocaleNegotiontionStrategy need java 1.8 or newer.%22,%0A                    e);%0A        %7D%0A%0A
     %7D%0A%7D%0A

Use reflection for java 1.8 classes