001 /*
002 * JBoss, Home of Professional Open Source.
003 * Copyright 2008, Red Hat Middleware LLC, and individual contributors
004 * as indicated by the @author tags. See the copyright.txt file in the
005 * distribution for a full listing of individual contributors.
006 *
007 * This is free software; you can redistribute it and/or modify it
008 * under the terms of the GNU Lesser General Public License as
009 * published by the Free Software Foundation; either version 2.1 of
010 * the License, or (at your option) any later version.
011 *
012 * This software is distributed in the hope that it will be useful,
013 * but WITHOUT ANY WARRANTY; without even the implied warranty of
014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015 * Lesser General Public License for more details.
016 *
017 * You should have received a copy of the GNU Lesser General Public
018 * License along with this software; if not, write to the Free
019 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
021 */
022 package org.jboss.dna.graph.commands.basic;
023
024 import java.util.Collection;
025 import java.util.List;
026 import net.jcip.annotations.NotThreadSafe;
027 import org.jboss.dna.common.util.StringUtil;
028 import org.jboss.dna.graph.commands.SetPropertiesCommand;
029 import org.jboss.dna.graph.properties.Path;
030 import org.jboss.dna.graph.properties.Property;
031
032 /**
033 * @author Randall Hauch
034 */
035 @NotThreadSafe
036 public class BasicSetPropertiesCommand extends BasicGraphCommand implements SetPropertiesCommand {
037
038 /**
039 */
040 private static final long serialVersionUID = -2693642411179501304L;
041 private final Path path;
042 private final List<Property> properties;
043
044 /**
045 * @param path the path to the node; may not be null
046 * @param properties the properties of the node; may not be null
047 */
048 public BasicSetPropertiesCommand( Path path,
049 List<Property> properties ) {
050 super();
051 assert path != null;
052 assert properties != null;
053 this.properties = properties;
054 this.path = path;
055 }
056
057 /**
058 * {@inheritDoc}
059 */
060 public Path getPath() {
061 return path;
062 }
063
064 /**
065 * {@inheritDoc}
066 */
067 public Collection<Property> getProperties() {
068 return properties;
069 }
070
071 /**
072 * {@inheritDoc}
073 *
074 * @see java.lang.Object#toString()
075 */
076 @Override
077 public String toString() {
078 StringBuilder sb = new StringBuilder();
079 sb.append(this.getClass().getSimpleName());
080 sb.append(" at ");
081 sb.append(this.getPath());
082 boolean firstProperty = true;
083 for (Property property : this.getProperties()) {
084 if (property.isEmpty()) continue;
085 if (firstProperty) {
086 sb.append(" { ");
087 firstProperty = false;
088 } else {
089 sb.append("; ");
090 }
091 sb.append(property.getName());
092 sb.append("=");
093 if (property.isSingle()) {
094 sb.append(StringUtil.readableString(property.getValues().next()));
095 } else {
096 sb.append(StringUtil.readableString(property.getValuesAsArray()));
097 }
098 }
099 if (!firstProperty) {
100 sb.append(" }");
101 }
102 return sb.toString();
103 }
104 }