2 * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
8 package org.opendaylight.protocol.pcep.impl;
10 import com.google.common.cache.Cache;
11 import com.google.common.cache.CacheBuilder;
13 import java.util.concurrent.TimeUnit;
15 import javax.annotation.concurrent.GuardedBy;
16 import javax.annotation.concurrent.ThreadSafe;
19 final class PeerRecord {
21 private final Cache<Short, Short> pastIds;
26 PeerRecord(final long idLifetimeSeconds, final Short lastId) {
27 // Note that the cache is limited to 255 entries -- which means we will always have
28 // a single entry available. That number will be the Last Recently Used ID.
29 pastIds = CacheBuilder.newBuilder().expireAfterWrite(idLifetimeSeconds, TimeUnit.SECONDS).maximumSize(255).build();
33 synchronized Short allocId() {
34 Short id = lastId == null ? 0 : lastId;
36 while (pastIds.getIfPresent(id) != null) {
37 id = (short) ((id + 1) % 255);