brooklyn-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From aledsage <...@git.apache.org>
Subject [GitHub] incubator-brooklyn pull request: BasicStartable entities transitio...
Date Thu, 06 Aug 2015 14:02:16 GMT
Github user aledsage commented on a diff in the pull request:

    https://github.com/apache/incubator-brooklyn/pull/798#discussion_r36417201
  
    --- Diff: core/src/test/java/org/apache/brooklyn/entity/basic/RecordingSensorEventListener.java
---
    @@ -0,0 +1,108 @@
    +/*
    + * 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.brooklyn.entity.basic;
    +
    +import java.util.Collections;
    +import java.util.Comparator;
    +import java.util.Iterator;
    +import java.util.List;
    +import java.util.Objects;
    +
    +import com.google.common.base.Function;
    +import com.google.common.collect.FluentIterable;
    +import com.google.common.collect.ImmutableList;
    +import com.google.common.collect.Lists;
    +import com.google.common.primitives.Longs;
    +
    +import brooklyn.event.SensorEvent;
    +import brooklyn.event.SensorEventListener;
    +
    +/**
    + * An event listener that records each event and allows callers to access all values
and
    + * all values sorted by event timestamp.
    + */
    +public class RecordingSensorEventListener<T> implements SensorEventListener<T>,
Iterable<SensorEvent<T>> {
    +
    +    private final List<SensorEvent<T>> events = Lists.newCopyOnWriteArrayList();
    +    private final boolean suppressDuplicates;
    +    private T lastValue;
    +
    +    public RecordingSensorEventListener() {
    +        this(false);
    +    }
    +
    +    public RecordingSensorEventListener(boolean suppressDuplicates) {
    +        this.suppressDuplicates = suppressDuplicates;
    +    }
    +
    +    @Override
    +    public void onEvent(SensorEvent<T> event) {
    +        if (!suppressDuplicates || events.isEmpty() || !Objects.equals(lastValue, event.getValue()))
{
    +            events.add(event);
    +            lastValue = event.getValue();
    +        }
    +    }
    +
    +    /**
    +     * @return An immutable iterable of the recorded events.
    +     */
    +    public Iterable<SensorEvent<T>> getEvents() {
    +        return ImmutableList.copyOf(events);
    +    }
    +
    +    public Iterable<T> getEventValues() {
    --- End diff --
    
    Deserves a comment that this is a live (read-only) view of the event values. At first
I didn't see how this worked with `Suppliers.ofInstance(listener.getEventValues())`, but after
looking at what a `FluentIterable`'s `iterator()` method does, this is an elegant solution!


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

Mime
View raw message