Github user grkvlt commented on a diff in the pull request:

    https://github.com/apache/brooklyn-server/pull/818#discussion_r138690532
  
    --- Diff: 
core/src/main/java/org/apache/brooklyn/core/objs/AbstractEntityAdjunct.java ---
    @@ -568,6 +570,98 @@ public void setUniqueTag(String uniqueTag) {
             return highlightsToReturn;
         }
     
    +    /** Records a named highlight against this object, for persistence and 
API access.
    +     * See common highlights including {@link #HIGHLIGHT_NAME_LAST_ACTION} 
and
    +     * {@link #HIGHLIGHT_NAME_LAST_CONFIRMATION}.
    +     * Also see convenience methods eg  {@link #highlightOngoing(String, 
String)} and {@link #highlight(String, String, Task)}
    +     * and {@link HighlightTuple}. 
    +     */
    +    protected void setHighlight(String name, HighlightTuple tuple) {
    +        highlights.put(name, tuple);
    +    }
    +
    +    /** As {@link #setHighlight(String, HighlightTuple)}, convenience for 
recording an item which is intended to be ongoing. */
    +    protected void highlightOngoing(String name, String description) {
    +        highlights.put(name, new HighlightTuple(description, 0, null));
    +    }
    +    /** As {@link #setHighlight(String, HighlightTuple)}, convenience for 
recording an item with the current time. */
    +    protected void highlightNow(String name, String description) {
    +        highlights.put(name, new HighlightTuple(description, 
System.currentTimeMillis(), null));
    +    }
    +    /** As {@link #setHighlight(String, HighlightTuple)}, convenience for 
recording an item with the current time and given task. */
    +    protected void highlight(String name, String description, @Nullable 
Task<?> t) {
    +        highlights.put(name, new HighlightTuple(description, 
System.currentTimeMillis(), t!=null ? t.getId() : null));
    +    }
    +    
    +    /** As {@link #setHighlight(String, HighlightTuple)} for {@link 
#HIGHLIGHT_NAME_TRIGGERS} (as ongoing). */
    +    protected void highlightTriggers(String description) {
    +        highlightOngoing(HIGHLIGHT_NAME_TRIGGERS, description);
    +    }
    +    protected <T> void highlightTriggers(Sensor<?> s, Object source) {
    +        highlightTriggers(Collections.singleton(s), 
Collections.singleton(source));
    +    }
    +    protected <T> void highlightTriggers(Iterable<? extends Sensor<? 
extends T>> s, Object source) {
    +        highlightTriggers(s, (Iterable<?>) (source instanceof Iterable ? 
(Iterable<?>)source : Collections.singleton(source)));
    +    }
    +    protected <U> void highlightTriggers(Sensor<?> s, Iterable<U> sources) 
{
    +        highlightTriggers(Collections.singleton(s), sources);
    +    }
    +    protected <T,U> void highlightTriggers(Iterable<? extends Sensor<? 
extends T>> sensors, Iterable<U> sources) {
    +        StringBuilder msg = new StringBuilder("Listening for ");
    +        boolean firstWord = true;
    +        if (sensors!=null) for (Object s: sensors) {
    +            if (s==null) continue;
    +            if (!firstWord) { msg.append(", "); } else { firstWord = 
false; }
    +            // s is normally a sensor but forgive other things if caller 
cheated generics
    +            msg.append(s instanceof Sensor ? ((Sensor<?>)s).getName() : 
s.toString());
    +        }
    +        if (firstWord) msg.append("<nothing>");
    +        
    +        firstWord = true;
    --- End diff --
    
    Interesting @Graeme-Miller - Not sure that you need the second clause in 
your `if`, as the body handles the `self` output the same as the original code. 
I'd prefer the `Collectors.joining(", ")` collector, to get a `String` out 
directly... Also, I'm not sure you should try and chest the lambda type 
inference like that, just go `s -> { /* stuff */ }` and it'll be fine. Redoing 
the whole method with lambdas gave me this, which is almost exactly the same 
length ;)
    
    ```
        protected <T,U> void highlightTriggers(Iterable<? extends Sensor<? 
extends T>> sensors, Iterable<U> sources) {
            StringBuilder msg = new StringBuilder("Listening for ");
            if (Iterables.isEmpty(sensors)) {
                msg.append("<nothing>");
            } else {
                String sensorsText = 
StreamSupport.stream(sensors.spliterator(), false)
                        .filter(s -> s != null)
                        .map(s -> {
                            if (s instanceof Sensor) {
                                return ((Sensor<?>) s).getName();
                            } else {
                                return s.toString();
                            }
                        })
                        .collect(Collectors.joining(", "));
                msg.append(sensorsText);
            }
            if (!Iterables.isEmpty(sources)) {
                String sourcesText = 
StreamSupport.stream(sources.spliterator(), false)
                        .filter(s -> s != null)
                        .map(s -> {
                            if (s.equals(getEntity())) {
                                return "self";
                            } else if (s instanceof Sensor) {
                                return ((Sensor<?>) s).getName();
                            } else {
                                return s.toString();
                            }
                        })
                        .collect(Collectors.joining(", "));
                msg.append(" on ")
                   .append(sourcesText);
            }
            highlightTriggers(msg.toString());
        }
    ```


---

Reply via email to