<commit_before>/*
 *  Licensed to GraphHopper GmbH under one or more contributor
 *  license agreements. See the NOTICE file distributed with this work for
 *  additional information regarding copyright ownership.
 *
 *  GraphHopper GmbH 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 com.graphhopper.storage;

import com.graphhopper.routing.util.AllEdgesIterator;
import com.graphhopper.routing.util.EdgeFilter;
import com.graphhopper.routing.util.EncodingManager;
import com.graphhopper.util.EdgeExplorer;
import com.graphhopper.util.EdgeIteratorState;
import com.graphhopper.util.shapes.BBox;

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

/**
 * This class manages all storage related methods and delegates the calls to the associated graphs.
 * The associated graphs manage their own necessary data structures and are used to provide e.g.
 * different traversal methods. By default this class implements the graph interface and results in
 * identical behavior as the Graph instance from getBaseGraph()
 * <p>
 *
 * @author <NAME>
 * @see GraphBuilder to create a (CH)Graph easier
 */
public final class GraphHopperStorage implements GraphStorage, Graph {
    private final Directory dir;
    private final EncodingManager encodingManager;
    private final StorableProperties properties;
    private final BaseGraph baseGraph;
    // same flush order etc
    private final Collection<CHGraphImpl> chGraphs;
    private final int segmentSize;

    public GraphHopperStorage(Directory dir, EncodingManager encodingManager, boolean withElevation) {
        this(dir, encodingManager, withElevation, false);
    }

    public GraphHopperStorage(Directory dir, EncodingManager encodingManager, boolean withElevation, boolean withTurnCosts) {
        this(dir, encodingManager, withElevation, withTurnCosts, -1);
    }

    public GraphHopperStorage(Directory dir, EncodingManager encodingManager, boolean withElevation, boolean withTurnCosts, int segmentSize) {
        if (encodingManager == null)
            throw new IllegalArgumentException("EncodingManager needs to be non-null since 0.7. Create one using EncodingManager.create or EncodingManager.create(flagEncoderFactory, ghLocation)");

        this.encodingManager = encodingManager;
        this.dir = dir;
        this.properties = new StorableProperties(dir);
        this.segmentSize = segmentSize;
        InternalGraphEventListener listener = new InternalGraphEventListener() {
            @Override
            public void initStorage() {
                for (CHGraphImpl cg : chGraphs) {
                    cg.initStorage();
                }
            }

            @Override
            public void freeze() {
                for (CHGraphImpl cg : chGraphs) {
                    cg._prepareForContraction();
                }
            }
        };
        baseGraph = new BaseGraph(dir, encodingManager, withElevation, listener, withTurnCosts, segmentSize);
        chGraphs = new ArrayList<>();
    }

    public void addCHGraph(CHProfile chProfile) {
        baseGraph.checkNotInitialized();
        if (getCHProfiles().contains(chProfile)) {
            throw new IllegalArgumentException("For the given CH profile a CHGraph already exists: " + chProfile);
        }
        chGraphs.add(new CHGraphImpl(chProfile, dir, baseGraph, segmentSize));
    }

    public void addCHGraphs(List<CHProfile> chProfiles) {
        for (CHProfile chProfile : chProfiles) {
            addCHGraph(chProfile);
        }
    }

    public CHGraph getCHGraph() {
        if (chGraphs.isEmpty()) {
            throw new IllegalStateException("There is no CHGraph");
        } else if (chGraphs.size() > 1) {
            throw new IllegalStateException("There are multiple CHGraphs, use getCHGraph(CHProfile) to retrieve a specific one");
        } else {
            return chGraphs.iterator().next();
        }
    }<commit_msg>Add java docs<commit_after>/*
 *  Licensed to GraphHopper GmbH under one or more contributor
 *  license agreements. See the NOTICE file distributed with this work for
 *  additional information regarding copyright ownership.
 *
 *  GraphHopper GmbH 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 com.graphhopper.storage;

import com.graphhopper.routing.util.AllEdgesIterator;
import com.graphhopper.routing.util.EdgeFilter;
import com.graphhopper.routing.util.EncodingManager;
import com.graphhopper.util.EdgeExplorer;
import com.graphhopper.util.EdgeIteratorState;
import com.graphhopper.util.shapes.BBox;

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

/**
 * This class manages all storage related methods and delegates the calls to the associated graphs.
 * The associated graphs manage their own necessary data structures and are used to provide e.g.
 * different traversal methods. By default this class implements the graph interface and results in
 * identical behavior as the Graph instance from getBaseGraph()
 * <p>
 *
 * @author <NAME>
 * @see GraphBuilder to create a (CH)Graph easier
 */
public final class GraphHopperStorage implements GraphStorage, Graph {
    private final Directory dir;
    private final EncodingManager encodingManager;
    private final StorableProperties properties;
    private final BaseGraph baseGraph;
    // same flush order etc
    private final Collection<CHGraphImpl> chGraphs;
    private final int segmentSize;

    public GraphHopperStorage(Directory dir, EncodingManager encodingManager, boolean withElevation) {
        this(dir, encodingManager, withElevation, false);
    }

    public GraphHopperStorage(Directory dir, EncodingManager encodingManager, boolean withElevation, boolean withTurnCosts) {
        this(dir, encodingManager, withElevation, withTurnCosts, -1);
    }

    public GraphHopperStorage(Directory dir, EncodingManager encodingManager, boolean withElevation, boolean withTurnCosts, int segmentSize) {
        if (encodingManager == null)
            throw new IllegalArgumentException("EncodingManager needs to be non-null since 0.7. Create one using EncodingManager.create or EncodingManager.create(flagEncoderFactory, ghLocation)");

        this.encodingManager = encodingManager;
        this.dir = dir;
        this.properties = new StorableProperties(dir);
        this.segmentSize = segmentSize;
        InternalGraphEventListener listener = new InternalGraphEventListener() {
            @Override
            public void initStorage() {
                for (CHGraphImpl cg : chGraphs) {
                    cg.initStorage();
                }
            }

            @Override
            public void freeze() {
                for (CHGraphImpl cg : chGraphs) {
                    cg._prepareForContraction();
                }
            }
        };
        baseGraph = new BaseGraph(dir, encodingManager, withElevation, listener, withTurnCosts, segmentSize);
        chGraphs = new ArrayList<>();
    }

    /**
     * Adds a {@link CHGraph} for the given {@link CHProfile}. You need to call this method before calling {@link #create(long)}
     * or {@link #loadExisting()}.
     */
    public void addCHGraph(CHProfile chProfile) {
        baseGraph.checkNotInitialized();
        if (getCHProfiles().contains(chProfile)) {
            throw new IllegalArgumentException("For the given CH profile a CHGraph already exists: " + chProfile);
        }
        chGraphs.add(new CHGraphImpl(chProfile, dir, baseGraph, segmentSize));
    }

    /**
     * @see #addCHGraph(CHProfile)
     */
    public void addCHGraphs(List<CHProfile> chProfiles) {
        for (CHProfile chProfile : chProfiles) {
            addCHGraph(chProfile);
        }
    }

    public CHGraph getCHGraph() {
        if (chGraphs.isEmpty()) {
            throw new IllegalStateException("There is no CHGraph");
        } else if (chGraphs.size() > 1) {
            throw new IllegalStateException("There are multiple CHGraphs, use getCHGraph(CHProfile) to retrieve a specific one");
        } else {
            return chGraphs.iterator().next();
        }
    }
{'fraction_non_alphanumeric': 0.07612344342176502, 'fraction_numerical': 0.0017325392528424472, 'mean_word_length': 3.125055828494864, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5044304', 'n_tokens_mistral': 2373, 'n_tokens_neox': 2304, 'n_words': 806}