/*
 * Copyright 2013 MovingBlocks
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.terasology.entitySystem.prefab.internal;

import com.google.api.client.repackaged.com.google.common.base.Strings;
import com.google.common.base.Preconditions;
import com.google.common.collect.Sets;
import org.terasology.assets.management.AssetManager;
import org.terasology.context.Context;
import org.terasology.entitySystem.Component;
import org.terasology.entitySystem.prefab.Prefab;
import org.terasology.entitySystem.prefab.PrefabManager;

import java.util.Collection;

/**
 * Basic implementation of PrefabManager.
 *
 * @author Immortius
 * @author Rasmus 'Cervator' Praestholm
 * @see PrefabManager
 */
public class PojoPrefabManager implements PrefabManager {

    private final AssetManager assetManager;

    public PojoPrefabManager(Context context) {
        this.assetManager = context.get(AssetManager.class);
    }


    /**
     * {@inheritDoc}
     */
    @Override
    public Prefab getPrefab(String name) {
        if (!name.isEmpty()) {
            Preconditions.checkArgument(!Strings.isNullOrEmpty(name));
            return assetManager.getAsset(name, Prefab.class).orElse(null);
        }
        return null;

    }

    /**
     * {@inheritDoc}
     */
    @Override
    public boolean exists(String name) {
        Preconditions.checkArgument(!Strings.isNullOrEmpty(name));
        return assetManager.getAsset(name, Prefab.class) != null;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public Iterable<Prefab> listPrefabs() {
        return assetManager.getLoadedAssets(Prefab.class);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public Collection<Prefab> listPrefabs(Class<? extends Component> comp) {
        Collection<Prefab> prefabs = Sets.newHashSet();

        for (Prefab p : assetManager.getLoadedAssets(Prefab.class)) {
            if (p.getComponent(comp) != null) {
                prefabs.add(p);
            }
        }

        return prefabs;
    }
}

/*
 * Copyright 2013 MovingBlocks
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.terasology.entitySystem.prefab.internal;

import com.google.api.client.repackaged.com.google.common.base.Strings;
import com.google.common.base.Preconditions;
import com.google.common.collect.Sets;
import org.terasology.assets.management.AssetManager;
import org.terasology.context.Context;
import org.terasology.entitySystem.Component;
import org.terasology.entitySystem.prefab.Prefab;
import org.terasology.entitySystem.prefab.PrefabManager;

import java.util.Collection;

/**
 * Basic implementation of PrefabManager.
 *
 * @author Immortius
 * @author Rasmus 'Cervator' Praestholm
 * @see PrefabManager
 */
public class PojoPrefabManager implements PrefabManager {

    private final AssetManager assetManager;

    public PojoPrefabManager(Context context) {
        this.assetManager = context.get(AssetManager.class);
    }


    /**
     * {@inheritDoc}
     */
    @Override
    public Prefab getPrefab(String name) {
        if (!name.isEmpty()) {
            Preconditions.checkArgument(!Strings.isNullOrEmpty(name));
            return assetManager.getAsset(name, Prefab.class).orElse(null);
        }
        return null;

    }

    /**
     * {@inheritDoc}
     */
    @Override
    public boolean exists(String name) {
        Preconditions.checkArgument(!Strings.isNullOrEmpty(name));
        return assetManager.getAsset(name, Prefab.class).isPresent();
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public Iterable<Prefab> listPrefabs() {
        return assetManager.getLoadedAssets(Prefab.class);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public Collection<Prefab> listPrefabs(Class<? extends Component> comp) {
        Collection<Prefab> prefabs = Sets.newHashSet();

        for (Prefab p : assetManager.getLoadedAssets(Prefab.class)) {
            if (p.getComponent(comp) != null) {
                prefabs.add(p);
            }
        }

        return prefabs;
    }
}

Fix PojoPrefabManager#exits which used to return always true.