/*
 * Copyright (C) 2003-2006, C. Ramakrishnan / Illposed Software.
 * All rights reserved.
 *
 * This code is licensed under the BSD 3-Clause license.
 * See file LICENSE (or LICENSE.html) for more information.
 */

package com.illposed.osc;

import com.illposed.osc.utility.OSCJavaToByteArrayConverter;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;

/**
 * An simple (non-bundle) OSC message.
 *
 * An OSC <i>Message</i> is made up of
 * an </i>Address Pattern</i> (the receiver of the message)
 * and </i>Arguments</i> (the content of the message).
 *
 * @author Chandrasekhar Ramakrishnan
 */
public class OSCMessage extends OSCPacket {

	private String address;
	private List<Object> arguments;

	/**
	 * Creates an empty OSC Message.
	 * In order to send this OSC message,
	 * you need to set the address and optionally some arguments.
	 */
	public OSCMessage() {
		this(null);
	}

	/**
	 * Creates an OSCMessage with an address already initialized.
	 * @param address  the recipient of this OSC message
	 */
	public OSCMessage(String address) {
		this(address, null);
	}

	/**
	 * Creates an OSCMessage with an address
	 * and arguments already initialized.
	 * @param address  the recipient of this OSC message
	 * @param arguments  the data sent to the receiver
	 */
	public OSCMessage(String address, Collection<Object> arguments) {

		this.address = address;
		if (arguments == null) {
			this.arguments = new LinkedList<Object>();
		} else {
			this.arguments = new ArrayList<Object>(arguments);
		}
		init();
	}

	/**
	 * The receiver of this message.
	 * @return the receiver of this OSC Message
	 */
	public String getAddress() {
		return address;
	}

	/**
	 * Set the address of this message.
	 * @param address the receiver of the message
	 */
	public void setAddress(String address) {
		this.address = address;
	}

	/**
	 * Add an argument to the list of arguments.
	 * @param argument a Float, String, Integer, BigInteger, Boolean
	 *   or an array of these
	 */
	public void addArgument(Object argument) {
		arguments.add(argument);
	}

	/**
	 * The arguments of this message.
	 * @return the arguments to this message
	 */
	public List<Object> getArguments() {
		return Collections.unmodifiableList(arguments);
	}

	/**
	 * Convert the address into a byte array.
	 * Used internally only.
	 */
	protected void computeAddressByteArray(OSCJavaToByteArrayConverter stream) {
		assert(address != null);
		stream.write(address);
	}

	/**
	 * Convert the arguments into a byte array.
	 * Used internally only.
	 */
	protected void computeArgumentsByteArray(OSCJavaToByteArrayConverter stream) {
		stream.write(',');
		stream.writeTypes(arguments);
		for (Object argument : arguments) {
			stream.write(argument);
		}
	}

	/**
	 * Convert the message into a byte array.
	 * Used internally only.
	 */
	protected byte[] computeByteArray(OSCJavaToByteArrayConverter stream) {
		computeAddressByteArray(stream);
		computeArgumentsByteArray(stream);
		return stream.toByteArray();
	}
}

@@ -436,16 +436,48 @@
 il.List;
+%0Aimport java.util.regex.Pattern;
 %0A%0A/**%0A *
@@ -760,16 +760,284 @@
 cket %7B%0A%0A
+%09/**%0A%09 * Java regular expression pattern matching a single invalid character.%0A%09 * The invalid characters are:%0A%09 * ' ', '#', '*', ',', '?', '%5B', '%5D', '%7B', '%7D'%0A%09 */%0A%09private static final Pattern ILLEGAL_ADDRESS_CHAR%0A%09%09%09= Pattern.compile(%22%5B %5C%5C#%5C%5C*%5C%5C,%5C%5C?%5C%5C%5B%5C%5C%5D%5C%5C%7B%5C%5C%7D%5D%22);%0A%0A
 %09private
@@ -3379,11 +3379,415 @@
 ay();%0A%09%7D
+%0A%0A%09/**%0A%09 * Checks whether a given string is a valid OSC %3Ci%3EAddress Pattern%3C/i%3E.%0A%09 * @param address to be checked for validity%0A%09 * @return true if the supplied string constitutes a valid OSC address%0A%09 */%0A%09public static boolean isValidAddress(String address) %7B%0A%09%09return (address != null)%0A%09%09%09%09&& address.startsWith(%22/%22)%0A%09%09%09%09&& !address.contains(%22//%22)%0A%09%09%09%09&& !ILLEGAL_ADDRESS_CHAR.matcher(address).find();%0A%09%7D
 %0A%7D%0A

allow to check whether a string is a valid OSC Adress