<commit_before>/*
 *    GeoTools - The Open Source Java GIS Toolkit
 *    http://geotools.org
 *
 *    (C) 2019, Open Source Geospatial Foundation (OSGeo)
 *
 *    This library is free software; you can redistribute it and/or
 *    modify it under the terms of the GNU Lesser General Public
 *    License as published by the Free Software Foundation;
 *    version 2.1 of the License.
 *
 *    This library is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *    Lesser General Public License for more details.
 *
 */

package org.geotools.tpk;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;

/**
 * TPKZoomLevel interface implementation for ESRI Compact Cache V1 files
 *
 * <p>TPK is a ZIP archive
 *
 * <p>In V1 the bundle index is a separate file from the bundle data and index entries are each
 * 5-bytes long. The index is stored in column-major order.
 */
public class TPKZoomLevelV1 implements TPKZoomLevel {

    // pattern isolates the "base" row and column number for each bundle (in hexidecimal)
    static Pattern PARSE_BUNDLE_NAME = Pattern.compile("^.*/R([0-9a-f]+)C([0-9a-f]+)\\.bundle$");

    static int BUNDLE_DIMENSION = 128; // bundle is 128 columns by 128 rows
    static int INDEX_ENTRY_LENGTH = 5; // each index entry is 5 bytes long
    static int INDEX_HEADER_LENGTH = 16; // first 16 bytes of index file is header data
    static int DATA_HEADER_LENGTH = 60; // first 60 bytes of bundle data file is header
    static int DATA_LENGTH_LENGTH = 4; // tile data length stored in 4 bytes
    static int HEXADECIMAL = 16; // row and column numbers in bundle name are base-16 encoded

    // if tile data offset is less than this value then tile does not exist!
    static int MINIMUM_DATA_OFFSET =
            DATA_HEADER_LENGTH
                    + BUNDLE_DIMENSION * BUNDLE_DIMENSION * DATA_LENGTH_LENGTH; // (65596)

    // injected via constructor
    private ZipFile theTPK;
    private Map<String, ZipEntry> zipEntryMap;

    // there are getters for these 5 members
    private final long zoomLevel; // zoom level number (0 to a max of 24)
    private long minRow; // minimum row number found in TPK for this zoom level
    private long maxRow; // maximum row number found in TPK for this zoom level
    private long minColumn; // minimum col number found in TPK for this zoom level
    private long maxColumn; // maximum col number found in TPK for this zoom level

    private long numTiles; // total number of tiles found in TPK for zoom level
    private long max_row_column; // max value for row and col (2** zoomLevel) -1
    private List<TPKBundle> bundles; // list of bundles for this zoom level

    public TPKZoomLevelV1(
            ZipFile theTPK, // TPK file is a ZIP file
            Map<String, ZipEntry> zipEntryMap, // map path/names to ZipEntry
            List<String> bundleNames, // names of bundle data files for this zoom level
            List<String> indexNames, // names of bundle index files for this zoom level
            long zoomLevel) {

        this.theTPK = theTPK;
        this.zipEntryMap = zipEntryMap;
        this.zoomLevel = zoomLevel;

        numTiles = 0;
        minColumn = Long.MAX_VALUE;
        maxColumn = Long.MIN_VALUE;
        minRow = Long.MAX_VALUE;
        maxRow = Long.MIN_VALUE;

        // maximum value for row and column at this zoom level
        max_row_column = new Double(Math.pow(2, zoomLevel) - 1).longValue();<commit_msg>same problem was in TpkZoomLevel1 implementation -- get rid of "new Double" and just use cast<commit_after>/*
 *    GeoTools - The Open Source Java GIS Toolkit
 *    http://geotools.org
 *
 *    (C) 2019, Open Source Geospatial Foundation (OSGeo)
 *
 *    This library is free software; you can redistribute it and/or
 *    modify it under the terms of the GNU Lesser General Public
 *    License as published by the Free Software Foundation;
 *    version 2.1 of the License.
 *
 *    This library is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *    Lesser General Public License for more details.
 *
 */

package org.geotools.tpk;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;

/**
 * TPKZoomLevel interface implementation for ESRI Compact Cache V1 files
 *
 * <p>TPK is a ZIP archive
 *
 * <p>In V1 the bundle index is a separate file from the bundle data and index entries are each
 * 5-bytes long. The index is stored in column-major order.
 */
public class TPKZoomLevelV1 implements TPKZoomLevel {

    // pattern isolates the "base" row and column number for each bundle (in hexidecimal)
    static Pattern PARSE_BUNDLE_NAME = Pattern.compile("^.*/R([0-9a-f]+)C([0-9a-f]+)\\.bundle$");

    static int BUNDLE_DIMENSION = 128; // bundle is 128 columns by 128 rows
    static int INDEX_ENTRY_LENGTH = 5; // each index entry is 5 bytes long
    static int INDEX_HEADER_LENGTH = 16; // first 16 bytes of index file is header data
    static int DATA_HEADER_LENGTH = 60; // first 60 bytes of bundle data file is header
    static int DATA_LENGTH_LENGTH = 4; // tile data length stored in 4 bytes
    static int HEXADECIMAL = 16; // row and column numbers in bundle name are base-16 encoded

    // if tile data offset is less than this value then tile does not exist!
    static int MINIMUM_DATA_OFFSET =
            DATA_HEADER_LENGTH
                    + BUNDLE_DIMENSION * BUNDLE_DIMENSION * DATA_LENGTH_LENGTH; // (65596)

    // injected via constructor
    private ZipFile theTPK;
    private Map<String, ZipEntry> zipEntryMap;

    // there are getters for these 5 members
    private final long zoomLevel; // zoom level number (0 to a max of 24)
    private long minRow; // minimum row number found in TPK for this zoom level
    private long maxRow; // maximum row number found in TPK for this zoom level
    private long minColumn; // minimum col number found in TPK for this zoom level
    private long maxColumn; // maximum col number found in TPK for this zoom level

    private long numTiles; // total number of tiles found in TPK for zoom level
    private long max_row_column; // max value for row and col (2** zoomLevel) -1
    private List<TPKBundle> bundles; // list of bundles for this zoom level

    public TPKZoomLevelV1(
            ZipFile theTPK, // TPK file is a ZIP file
            Map<String, ZipEntry> zipEntryMap, // map path/names to ZipEntry
            List<String> bundleNames, // names of bundle data files for this zoom level
            List<String> indexNames, // names of bundle index files for this zoom level
            long zoomLevel) {

        this.theTPK = theTPK;
        this.zipEntryMap = zipEntryMap;
        this.zoomLevel = zoomLevel;

        numTiles = 0;
        minColumn = Long.MAX_VALUE;
        maxColumn = Long.MIN_VALUE;
        minRow = Long.MAX_VALUE;
        maxRow = Long.MIN_VALUE;

        // maximum value for row and column at this zoom level
        max_row_column = (long) Math.pow(2, zoomLevel) - 1;
{'fraction_non_alphanumeric': 0.0703698758178662, 'fraction_numerical': 0.01455468019762318, 'mean_word_length': 2.846944016435542, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '1795121', 'n_tokens_mistral': 2209, 'n_tokens_neox': 2100, 'n_words': 917}