ParameterManager.java
Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2011 Google Inc.
00003  * 
00004  * Licensed under the Apache License, Version 2.0 (the "License"); you may not
00005  * use this file except in compliance with the License. You may obtain a copy of
00006  * the License at
00007  * 
00008  * http://www.apache.org/licenses/LICENSE-2.0
00009  * 
00010  * Unless required by applicable law or agreed to in writing, software
00011  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
00012  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
00013  * License for the specific language governing permissions and limitations under
00014  * the License.
00015  */
00016 
00017 package org.ros.internal.node.parameter;
00018 
00019 import com.google.common.collect.Maps;
00020 
00021 import org.ros.concurrent.ListenerGroup;
00022 import org.ros.concurrent.SignalRunnable;
00023 import org.ros.namespace.GraphName;
00024 import org.ros.node.parameter.ParameterListener;
00025 
00026 import java.util.Map;
00027 import java.util.concurrent.ExecutorService;
00028 
00032 public class ParameterManager {
00033 
00034   private final ExecutorService executorService;
00035   private final Map<GraphName, ListenerGroup<ParameterListener>> listeners;
00036 
00037   public ParameterManager(ExecutorService executorService) {
00038     this.executorService = executorService;
00039     listeners = Maps.newHashMap();
00040   }
00041 
00042   public void addListener(GraphName parameterName, ParameterListener listener) {
00043     synchronized (listeners) {
00044       if (!listeners.containsKey(parameterName)) {
00045         listeners.put(parameterName, new ListenerGroup<ParameterListener>(executorService));
00046       }
00047       listeners.get(parameterName).add(listener);
00048     }
00049   }
00050 
00056   public int updateParameter(GraphName parameterName, final Object value) {
00057     int numberOfListeners = 0;
00058     synchronized (listeners) {
00059       if (listeners.containsKey(parameterName)) {
00060         ListenerGroup<ParameterListener> listenerCollection = listeners.get(parameterName);
00061         numberOfListeners = listenerCollection.size();
00062         listenerCollection.signal(new SignalRunnable<ParameterListener>() {
00063           @Override
00064           public void run(ParameterListener listener) {
00065             listener.onNewValue(value);
00066           }
00067         });
00068       }
00069     }
00070     return numberOfListeners;
00071   }
00072 }


rosjava_core
Author(s):
autogenerated on Wed Aug 26 2015 16:06:49