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