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

com.highmobility.autoapi.MultiCommand Maven / Gradle / Ivy

/*
 * The MIT License
 * 
 * Copyright (c) 2014- High-Mobility GmbH (https://high-mobility.com)
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */
package com.highmobility.autoapi;

import com.highmobility.autoapi.property.Property;
import java.util.ArrayList;
import java.util.List;

/**
 * The Multi Command capability
 */
public class MultiCommand {
    public static final int IDENTIFIER = Identifier.MULTI_COMMAND;

    public static final byte PROPERTY_MULTI_STATES = 0x01;
    public static final byte PROPERTY_MULTI_COMMANDS = 0x02;

    /**
     * Multi command command
     */
    public static class MultiCommandCommand extends SetCommand {
        List> multiCommands;
    
        /**
         * @return The multi commands
         */
        public List> getMultiCommands() {
            return multiCommands;
        }
        
        /**
         * Multi command command
         * 
         * @param multiCommands The outgoing commands
         */
        public MultiCommandCommand(List multiCommands) {
            super(IDENTIFIER);
        
            final ArrayList> multiCommandsBuilder = new ArrayList<>();
            if (multiCommands != null) {
                for (Command multiCommand : multiCommands) {
                    Property prop = new Property<>(0x02, multiCommand);
                    multiCommandsBuilder.add(prop);
                    addProperty(prop);
                }
            }
            this.multiCommands = multiCommandsBuilder;
            createBytes();
        }
    
        MultiCommandCommand(byte[] bytes) throws CommandParseException, NoPropertiesException {
            super(bytes);
        
            final ArrayList> multiCommandsBuilder = new ArrayList<>();
        
            while (propertyIterator.hasNext()) {
                propertyIterator.parseNext(p -> {
                    if (p.getPropertyIdentifier() == PROPERTY_MULTI_COMMANDS) {
                        Property multiCommand = new Property<>(Command.class, p);
                        multiCommandsBuilder.add(multiCommand);
                        return multiCommand;
                    }
                    return null;
                });
            }
        
            multiCommands = multiCommandsBuilder;
            if (this.multiCommands.size() == 0) 
                throw new NoPropertiesException();
        }
    }

    /**
     * The multi command state
     */
    public static class State extends SetCommand {
        List> multiStates;
    
        /**
         * @return The incoming states
         */
        public List> getMultiStates() {
            return multiStates;
        }
    
        State(byte[] bytes) throws CommandParseException {
            super(bytes);
    
            final ArrayList> multiStatesBuilder = new ArrayList<>();
    
            while (propertyIterator.hasNext()) {
                propertyIterator.parseNext(p -> {
                    switch (p.getPropertyIdentifier()) {
                        case PROPERTY_MULTI_STATES:
                            Property multiState = new Property<>(Command.class, p);
                            multiStatesBuilder.add(multiState);
                            return multiState;
                    }
    
                    return null;
                });
            }
    
            multiStates = multiStatesBuilder;
        }
    
        private State(Builder builder) {
            super(builder);
    
            multiStates = builder.multiStates;
        }
    
        public static final class Builder extends SetCommand.Builder {
            private final List> multiStates = new ArrayList<>();
    
            public Builder() {
                super(IDENTIFIER);
            }
    
            public State build() {
                return new State(this);
            }
    
            /**
             * Add an array of multi states.
             * 
             * @param multiStates The multi states. The incoming states
             * @return The builder
             */
            public Builder setMultiStates(Property[] multiStates) {
                this.multiStates.clear();
                for (int i = 0; i < multiStates.length; i++) {
                    addMultiState(multiStates[i]);
                }
            
                return this;
            }
            /**
             * Add a single multi state.
             * 
             * @param multiState The multi state. The incoming states
             * @return The builder
             */
            public Builder addMultiState(Property multiState) {
                multiState.setIdentifier(PROPERTY_MULTI_STATES);
                addProperty(multiState);
                multiStates.add(multiState);
                return this;
            }
        }
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy