/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you 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.apache.jackrabbit.oak.plugins.index.lucene.hybrid;

import java.util.Iterator;

import org.apache.commons.collections4.MultiValuedMap;
import org.apache.jackrabbit.oak.commons.collections.IteratorUtils;
import org.apache.jackrabbit.oak.plugins.document.spi.JournalProperty;

import static java.util.Objects.requireNonNull;

class IndexedPaths implements JournalProperty, Iterable<IndexedPathInfo> {
    private final MultiValuedMap<String, String> indexedPaths;

    public IndexedPaths(MultiValuedMap<String, String> indexedPaths) {
        this.indexedPaths = requireNonNull(indexedPaths);
    }

    @Override
    public Iterator<IndexedPathInfo> iterator() {
        return IteratorUtils.transform(indexedPaths.asMap().entrySet().iterator(), input ->
            new IndexedPathInfo() {
                @Override
                public String getPath() {
                    return input.getKey();
                }

                @Override
                public Iterable<String> getIndexPaths() {
                    return input.getValue();
                }
            });
    }

    @Override
    public String toString() {
        return indexedPaths.toString();
    }

    public boolean isEmpty(){
        return indexedPaths.isEmpty();
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        IndexedPaths that = (IndexedPaths) o;

        return indexedPaths.equals(that.indexedPaths);
    }

    @Override
    public int hashCode() {
        return 0;
    }
}
