package com.github.dockerjava.api.model;

import org.apache.commons.lang.builder.EqualsBuilder;
import org.apache.commons.lang.builder.HashCodeBuilder;

public class Bind {

	private String path;

	private Volume volume;

	private boolean readOnly = false;

	public Bind(String path, Volume volume) {
		this(path, volume, false);
	}

	public Bind(String path, Volume volume, boolean readOnly) {
		this.path = path;
		this.volume = volume;
		this.readOnly = readOnly;
	}

	public String getPath() {
		return path;
	}

	public Volume getVolume() {
		return volume;
	}

	public boolean isReadOnly() {
		return readOnly;
	}

	public static Bind parse(String serialized) {
		try {
			String[] parts = serialized.split(":");
			switch (parts.length) {
			case 2: {
				return new Bind(parts[0], Volume.parse(parts[1]));
			}
			case 3: {
				if ("rw".equals(parts[3].toLowerCase()))
					return new Bind(parts[0], Volume.parse(parts[1]), true);
				else
					throw new RuntimeException("Error parsing Bind '"
							+ serialized + "'");
			}
			default: {
				throw new RuntimeException("Error parsing Bind '" + serialized
						+ "'");
			}
			}
		} catch (Exception e) {
			throw new RuntimeException("Error parsing Bind '" + serialized
					+ "'");
		}
	}

	@Override
	public boolean equals(Object obj) {
		if (obj instanceof Bind) {
			Bind other = (Bind) obj;
			return new EqualsBuilder().append(path, other.getPath())
					.append(volume, other.getVolume())
					.append(readOnly, other.isReadOnly()).isEquals();
		} else
			return super.equals(obj);
	}

	@Override
	public int hashCode() {
		return new HashCodeBuilder().append(path).append(volume)
				.append(readOnly).toHashCode();
	}
}

@@ -606,32 +606,237 @@
 n readOnly;%0A%09%7D%0A%0A
+%09/**%0A%09 * Parses a bind mount specification to a %7B@link Bind%7D.%0A%09 * %0A%09 * @param serialized the specification, e.g. %3Ccode%3E/host:/container:ro%3C/code%3E%0A%09 * @return a %7B@link Bind%7D matching the specification%0A%09 */%0A
 %09public static B
@@ -866,16 +866,16 @@
 ized) %7B%0A
-
 %09%09try %7B%0A
@@ -1056,17 +1056,130 @@
 s(parts%5B
-3
+2%5D.toLowerCase()))%0A%09%09%09%09%09return new Bind(parts%5B0%5D, Volume.parse(parts%5B1%5D), false);%0A%09%09%09%09else if (%22ro%22.equals(parts%5B2
 %5D.toLowe

Fix and improve parsing of serialized Bind