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

edu.uvm.ccts.common.db.parser.MapTableIdRegistry Maven / Gradle / Ivy

Go to download

A library of useful generic objects and tools consolidated here to simplify all UVM CCTS projects

There is a newer version: 1.1.5
Show newest version
/*
 * Copyright 2015 The University of Vermont and State
 * Agricultural College.  All rights reserved.
 *
 * Written by Matthew B. Storer 
 *
 * This file is part of CCTS Common.
 *
 * CCTS Common is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * CCTS Common 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with CCTS Common.  If not, see .
 */

package edu.uvm.ccts.common.db.parser;

import java.security.NoSuchAlgorithmException;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by mstorer on 5/13/14.
 */
public class MapTableIdRegistry {
    private static MapTableIdRegistry registry = null;

    public static MapTableIdRegistry getInstance() {
        if (registry == null) registry = new MapTableIdRegistry();
        return registry;
    }


//////////////////////////////////////////////////////////////////////////////////
// instance methods
//

    private Map> tableKeyIdMap = new HashMap>();

    private MapTableIdRegistry() {}

    public void reset() {
        tableKeyIdMap.clear();
    }

    public boolean hasId(String tableName, String key) throws NoSuchAlgorithmException {
        synchronized(tableName.intern()) {
            Map keyIdMap = tableKeyIdMap.get(tableName);

            return keyIdMap != null && keyIdMap.containsKey(key);
        }
    }

    public Integer generateId(String tableName, String key) throws NoSuchAlgorithmException {
        synchronized(tableName.intern()) {
            Map keyIdMap = tableKeyIdMap.get(tableName);
            if (keyIdMap == null) {
                keyIdMap = new HashMap();
                tableKeyIdMap.put(tableName, keyIdMap);
            }

            Integer id = keyIdMap.get(key);
            if (id == null) {
                id = IdGenerator.getInstance().nextId(tableName);
                keyIdMap.put(key, id);
            }

            return id;
        }
    }

    public Integer getId(String tableName, String key) throws NoSuchAlgorithmException {
        synchronized(tableName.intern()) {
            Map keyIdMap = tableKeyIdMap.get(tableName);

            return keyIdMap != null ?
                    keyIdMap.get(key) :
                    null;
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy