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

ws.wamp.jawampa.internal.IdGenerator Maven / Gradle / Ivy

/*
 * Copyright 2014 Matthias Einwag
 *
 * The jawampa authors license 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 ws.wamp.jawampa.internal;

import java.util.Map;
import java.util.concurrent.ThreadLocalRandom;

/**
 * Contains method for generating WAMP IDs
 */
public class IdGenerator {
    
    /**
     * Generates a new ID through a random generator.
* If the new ID is not valid or is already in use as a key in the provided Map * the process will be repeated until a valid iD can be returned. * @param controlMap The map which should be optionally checked. If the parameter * is not null and contains a key with the new ID value then the ID will not be used. * @return The new ID */ public static long newRandomId(Map controlMap) { for (;;) { long l = ThreadLocalRandom.current().nextLong(); if (l < IdValidator.MIN_VALID_ID || l > IdValidator.MAX_VALID_ID) continue; if (controlMap == null || !controlMap.containsKey(l)) return l; } } /** * Generates a new ID by incrementing an old one.
* If the new ID is not valid or is already in use as a key in the provided Map * the process will be repeated until a valid iD can be returned. * @param startValue The starting value for the process. The returned ID will be * startValue + 1 unless that value is not valid or already in use. * @param controlMap The map which should be optionally checked. If the parameter * is not null and contains a key with the new ID value then the ID will not be used. * @return The new ID */ public static long newLinearId(long startValue, Map controlMap) { long val = startValue; for (;;) { val++; if (val > IdValidator.MAX_VALID_ID) val = IdValidator.MIN_VALID_ID; if (controlMap == null || !controlMap.containsKey(val)) return val; } } }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy