001/*******************************************************************************
002 * Copyright (c) 2017 Red Hat Inc and others.
003 *
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the Eclipse Public License v1.0
006 * which accompanies this distribution, and is available at
007 * http://www.eclipse.org/legal/epl-v10.html
008 *
009 * Contributors:
010 *     Red Hat Inc - initial API and implementation
011 *******************************************************************************/
012package org.eclipse.kapua.gateway.client;
013
014import static java.time.Instant.now;
015import static java.util.Collections.singletonMap;
016import static java.util.Collections.unmodifiableMap;
017
018import java.time.Instant;
019import java.util.HashMap;
020import java.util.Map;
021
022/**
023 * Payload data
024 */
025public class Payload {
026
027    public static class Builder {
028
029        private Instant timestamp;
030
031        private Map<String, Object> values = new HashMap<>();
032
033        public Builder() {
034            this.timestamp = Instant.now();
035        }
036
037        public Instant timestamp() {
038            return this.timestamp;
039        }
040
041        public Builder timestamp(final Instant timestamp) {
042            this.timestamp = timestamp;
043            return this;
044        }
045
046        public Map<String, Object> values() {
047            return this.values;
048        }
049
050        public Builder values(final Map<String, Object> values) {
051            this.values = values;
052            return this;
053        }
054
055        public Builder put(final String key, final Object value) {
056            this.values.put(key, value);
057            return this;
058        }
059
060        public Payload build() {
061            return new Payload(this.timestamp, this.values, true);
062        }
063    }
064
065    private final Instant timestamp;
066    private final Map<String, Object> values;
067
068    private Payload(final Instant timestamp, final Map<String, Object> values, final boolean cloneValues) {
069        this.timestamp = timestamp;
070        this.values = unmodifiableMap(cloneValues ? new HashMap<>(values) : values);
071    }
072
073    public Instant getTimestamp() {
074        return this.timestamp;
075    }
076
077    public Map<String, Object> getValues() {
078        return this.values;
079    }
080
081    @Override
082    public String toString() {
083        return String.format("[Payload - timestamp: %s, values: %s]", this.timestamp, this.values);
084    }
085
086    public static Payload of(final String key, final Object value) {
087        return new Payload(now(), singletonMap(key, value), false);
088    }
089
090    public static Payload of(final Map<String, Object> values) {
091        return new Payload(now(), values, true);
092    }
093
094    public static Payload of(final Instant timestamp, final String key, final Object value) {
095        return new Payload(timestamp, singletonMap(key, value), false);
096    }
097
098    public static Payload of(final Instant timestamp, final Map<String, Object> values) {
099        return new Payload(timestamp, values, true);
100    }
101}