All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.vtence.molecule.http.ContentLanguage Maven / Gradle / Ivy

There is a newer version: 0.15.0
Show newest version
package com.vtence.molecule.http;

import com.vtence.molecule.Response;
import com.vtence.molecule.helpers.Joiner;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Locale;

import static com.vtence.molecule.http.HeaderNames.CONTENT_LANGUAGE;
import static java.util.stream.Collectors.toList;

public class ContentLanguage {
    private final List locales = new ArrayList<>();

    public static ContentLanguage of(Response response) {
        String header = response.header(CONTENT_LANGUAGE);
        return header != null ? ContentLanguage.parse(header) : null;
    }

    public static ContentLanguage parse(String header) {
        return from(new Header(header));
    }

    public static ContentLanguage from(Header header) {
        ContentLanguage languages = new ContentLanguage();
        header.values().stream().filter(locale -> !locale.equals(""))
              .forEach(locale -> languages.add(Locale.forLanguageTag(locale)));
        return languages;
    }

    public ContentLanguage add(Locale locale) {
        this.locales.add(locale);
        return this;
    }

    public List locales() {
        return new ArrayList<>(locales);
    }

    public ContentLanguage remove(Locale locale) {
        locales.remove(locale);
        return this;
    }

    public String toString() {
        return Joiner.on(", ").join(format(locales));
    }

    private Iterable format(Collection locales) {
        return locales.stream().map(Locale::toLanguageTag).collect(toList());
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy