001/*
002 * Copyright (c) 2007-2015 Concurrent, Inc. All Rights Reserved.
003 *
004 * Project and contact information: http://www.cascading.org/
005 *
006 * This file is part of the Cascading project.
007 *
008 * Licensed under the Apache License, Version 2.0 (the "License");
009 * you may not use this file except in compliance with the License.
010 * You may obtain a copy of the License at
011 *
012 *     http://www.apache.org/licenses/LICENSE-2.0
013 *
014 * Unless required by applicable law or agreed to in writing, software
015 * distributed under the License is distributed on an "AS IS" BASIS,
016 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
017 * See the License for the specific language governing permissions and
018 * limitations under the License.
019 */
020
021package cascading.property;
022
023import java.util.Set;
024import java.util.TreeSet;
025
026import cascading.util.Util;
027import org.slf4j.Logger;
028import org.slf4j.LoggerFactory;
029
030import static cascading.util.Util.join;
031
032/** Class UnitOfWorkDef is the base class for framework specific fluent definition interfaces. */
033public class UnitOfWorkDef<T>
034  {
035  private static final Logger LOG = LoggerFactory.getLogger( UnitOfWorkDef.class );
036
037  protected String name;
038  protected Set<String> tags = new TreeSet<String>();
039
040  public UnitOfWorkDef()
041    {
042    }
043
044  protected UnitOfWorkDef( UnitOfWorkDef<T> unitOfWorkDef )
045    {
046    this.name = unitOfWorkDef.name;
047    this.tags.addAll( unitOfWorkDef.tags );
048    }
049
050  public String getName()
051    {
052    return name;
053    }
054
055  /**
056   * Method setName sets the UnitOfWork name.
057   *
058   * @param name type String
059   * @return this
060   */
061  public T setName( String name )
062    {
063    this.name = name;
064    return (T) this;
065    }
066
067  public String getTags()
068    {
069    return join( tags, "," );
070    }
071
072  /**
073   * Method addTag will associate a "tag" with this UnitOfWork. A UnitOfWork can have an unlimited number of tags.
074   * <p/>
075   * Tags allow for search and organization by management tools.
076   * <p/>
077   * Tag values are opaque, but adopting a simple convention of 'category:value' allows for complex use cases.
078   * <p/>
079   * Note that tags should not contain whitespace characters, even though this is not an error, a warning will be
080   * issues.
081   *
082   * @param tag type String
083   * @return this
084   */
085  public T addTag( String tag )
086    {
087    if( tag == null || tag.isEmpty() )
088      return (T) this;
089
090    tag = tag.trim();
091
092    if( Util.containsWhitespace( tag ) )
093      LOG.warn( "tags should not contain whitespace characters: '{}'", tag );
094
095    tags.add( tag );
096
097    return (T) this;
098    }
099
100  /**
101   * Method addTags will associate the given "tags" with this UnitOfWork. A UnitOfWork can have an unlimited number of tags.
102   * <p/>
103   * Tags allow for search and organization by management tools.
104   * <p/>
105   * Tag values are opaque, but adopting a simple convention of 'category:value' allows for complex use cases.
106   * <p/>
107   * Note that tags should not contain whitespace characters, even though this is not an error, a warning will be
108   * issues.
109   *
110   * @param tags type String
111   * @return this
112   */
113  public T addTags( String... tags )
114    {
115    for( String tag : tags )
116      addTag( tag );
117
118    return (T) this;
119    }
120  }