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

org.apache.zeppelin.display.AngularObject Maven / Gradle / Ivy

There is a newer version: 0.11.2
Show newest version
/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses 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 org.apache.zeppelin.display;

import java.util.LinkedList;
import java.util.List;
import java.util.concurrent.ExecutorService;

import org.apache.zeppelin.scheduler.ExecutorFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 *
 *
 * @param 
 */
public class AngularObject {
  private String name;
  private T object;
  private transient AngularObjectListener listener;
  private transient List watchers
    = new LinkedList();

  protected AngularObject(String name, T o,
      AngularObjectListener listener) {
    this.name = name;
    this.listener = listener;
    object = o;
  }

  public String getName() {
    return name;
  }

  @Override
  public boolean equals(Object o) {
    if (o instanceof AngularObject) {
      return name.equals(((AngularObject) o).name);
    } else {
      return false;
    }
  }

  public Object get() {
    return object;
  }

  public void emit(){
    if (listener != null) {
      listener.updated(this);
    }
  }

  public void set(T o) {
    set(o, true);
  }

  public void set(T o, boolean emit) {
    final T before = object;
    final T after = o;
    object = o;
    if (emit) {
      emit();
    }

    final Logger logger = LoggerFactory.getLogger(AngularObject.class);
    List ws = new LinkedList();
    synchronized (watchers) {
      ws.addAll(watchers);
    }

    ExecutorService executor = ExecutorFactory.singleton().createOrGet("angularObjectWatcher", 50);
    for (final AngularObjectWatcher w : ws) {
      executor.submit(new Runnable() {
        @Override
        public void run() {
          try {
            w.watch(before, after);
          } catch (Exception e) {
            logger.error("Exception on watch", e);
          }
        }
      });
    }
  }

  public void setListener(AngularObjectListener listener) {
    this.listener = listener;
  }

  public AngularObjectListener getListener() {
    return listener;
  }

  public void addWatcher(AngularObjectWatcher watcher) {
    synchronized (watchers) {
      watchers.add(watcher);
    }
  }

  public void removeWatcher(AngularObjectWatcher watcher) {
    synchronized (watchers) {
      watchers.remove(watcher);
    }
  }

  public void clearAllWatchers() {
    synchronized (watchers) {
      watchers.clear();
    }
  }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy