1 /* 2 * (c) Copyright 2006-2020 by rapiddweller GmbH & Volker Bergmann. All rights reserved. 3 * 4 * Redistribution and use in source and binary forms, with or without 5 * modification, is permitted under the terms of the 6 * GNU General Public License. 7 * 8 * For redistributing this software or a derivative work under a license other 9 * than the GPL-compatible Free Software License as defined by the Free 10 * Software Foundation or approved by OSI, you must first obtain a commercial 11 * license to this software product from rapiddweller GmbH & Volker Bergmann. 12 * 13 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 14 * WITHOUT A WARRANTY OF ANY KIND. ALL EXPRESS OR IMPLIED CONDITIONS, 15 * REPRESENTATIONS AND WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF 16 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE 17 * HEREBY EXCLUDED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 18 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 19 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 20 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 21 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 22 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 23 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 24 * POSSIBILITY OF SUCH DAMAGE. 25 */ 26 27 package com.rapiddweller.benerator.wrapper; 28 29 import com.rapiddweller.benerator.Generator; 30 31 import java.util.HashMap; 32 import java.util.Map; 33 34 /** 35 * Helper class for the {@link Generator} class.<br/><br/> 36 * Created: 26.01.2010 10:53:53 37 * 38 * @param <E> the type parameter 39 * @author Volker Bergmann 40 * @since 0.6.0 41 */ 42 public class ProductWrapper<E> { 43 44 private E product; 45 private Map<String, String> tags; 46 47 /** 48 * Instantiates a new Product wrapper. 49 * 50 * @param product the product 51 */ 52 public ProductWrapper(E product) { 53 this(); 54 wrap(product); 55 } 56 57 /** 58 * Instantiates a new Product wrapper. 59 */ 60 public ProductWrapper() { 61 this.tags = null; 62 } 63 64 /** 65 * Wrap product wrapper. 66 * 67 * @param product the product 68 * @return the product wrapper 69 */ 70 public ProductWrapper<E> wrap(E product) { 71 return wrap(product, true); 72 } 73 74 /** 75 * Wrap product wrapper. 76 * 77 * @param product the product 78 * @param clearTags the clear tags 79 * @return the product wrapper 80 */ 81 public ProductWrapper<E> wrap(E product, boolean clearTags) { 82 this.product = product; 83 if (tags != null && clearTags) { 84 tags.clear(); 85 } 86 return this; 87 } 88 89 /** 90 * Unwrap e. 91 * 92 * @return the e 93 */ 94 public E unwrap() { 95 return this.product; 96 } 97 98 /** 99 * Gets tag. 100 * 101 * @param key the key 102 * @return the tag 103 */ 104 public String getTag(String key) { 105 return (tags != null ? tags.get(key) : null); 106 } 107 108 /** 109 * Sets tag. 110 * 111 * @param key the key 112 * @param value the value 113 * @return the tag 114 */ 115 public ProductWrapper<E> setTag(String key, String value) { 116 if (tags == null) { 117 tags = new HashMap<>(); 118 } 119 tags.put(key, value); 120 return this; 121 } 122 123 @Override 124 public String toString() { 125 return String.valueOf(product); 126 } 127 128 /** 129 * Unwrap object. 130 * 131 * @param wrapper the wrapper 132 * @return the object 133 */ 134 public static Object unwrap(ProductWrapper<?> wrapper) { 135 return (wrapper != null ? wrapper.product : null); 136 } 137 138 }