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

com.github.fscheffer.arras.test.services.ArrasTestModule Maven / Gradle / Ivy

package com.github.fscheffer.arras.test.services;

import java.net.URL;
import java.util.List;
import java.util.Map;

import org.apache.tapestry5.ioc.internal.util.CollectionFactory;
import org.slf4j.Logger;

import com.github.fscheffer.arras.test.Track;

public class ArrasTestModule {

    // from tapestry5-core
    public MusicLibrary buildMusicLibrary(Logger log) {

        URL library = getClass().getResource("iTunes.xml");

        final List tracks = new MusicLibraryParser(log).parseTracks(library);

        final Map idToTrack = CollectionFactory.newMap();

        for (Track t : tracks) {
            idToTrack.put(t.getId(), t);
        }

        return new MusicLibrary() {

            @Override
            public Track getById(long id) {
                Track result = idToTrack.get(id);

                if (result != null) {
                    return result;
                }

                throw new IllegalArgumentException(String.format("No track with id #%d.", id));
            }

            @Override
            public List getTracks() {
                return tracks;
            }

            @Override
            public List findByMatchingTitle(String title) {
                String titleLower = title.toLowerCase();

                List result = CollectionFactory.newList();

                for (Track t : tracks) {
                    if (t.getTitle().toLowerCase().contains(titleLower)) {
                        result.add(t);
                    }
                }

                return result;
            }
        };
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy