View Javadoc

1   package org.musicontroller.core;
2   
3   import java.util.Date;
4   
5   import org.musicontroller.model.Linkable;
6   
7   /**
8    * Basic functionality for the Linkable objects. In addition to the Linkable
9    * interface, the LinkableAbs has a changed and an inserted-property.
10   * @author Varienaja
11   */
12  public abstract class LinkableAbs implements Linkable {
13  	private long _id;
14  	private String _name;
15  	private Date _changed;
16  	private Date _inserted;
17  	
18  	/**
19  	 * Constructs a LinkableAbs, with empty Name (zero length, but not null), an
20  	 *  Id of -1, Changed to null and Inserted to the current timestamp.
21  	 */
22  	public LinkableAbs() {
23  		_changed = null;
24  		_inserted=new Date();
25  		_id = -1;
26  		_name = "";
27  	}
28  	
29  	/*
30  	 * (non-Javadoc)
31  	 * @see org.musicontroller.model.Linkable#getId()
32  	 */
33  	public long getId() {
34  		return _id;
35  	}
36  	
37  	public void setId(long id) {
38  		_id = id;
39  	}
40  	
41  	/*
42  	 * (non-Javadoc)
43  	 * @see org.musicontroller.model.Linkable#getName()
44  	 */
45  	public String getName() {
46  		return _name;
47  	}
48  	
49  	/**
50  	 * Takes a Name (an ordinary String), and returns the normalized version.
51  	 * The normalized version contains a maximum of 150 characters, and has all
52  	 * leading and trailing whitespace removed.
53  	 * @param name The Name.
54  	 * @return The normalized Name.
55  	 */
56  	public static String normalizeName(String name) {
57  		if (name==null) {
58  			return "";
59  		} else {
60  			name = name.trim(); //Throw useless characters overboard
61  			if (name.length()<=150) {
62  			return name;
63  			} else {
64  				return name.substring(0,150);
65  			}
66  		}
67  	}
68  	
69  	/**
70  	 * Setter for the name. Null values will be understood as empty strings. All
71  	 * input will be trimmed (trailing and leading whitespace will be removed),
72  	 * when the resulting string is longer than 150 characters, it will be
73  	 * cut-off.
74  	 * @param name The name to set.
75  	 */
76  	public void setName(String name) {
77  		_name = normalizeName(name);
78  	}
79  	
80  	/**
81  	 * Returns the date when this object was last-changed. The changed-property
82  	 * is intended to be used by Hibernate for providing optimistic locking.
83  	 * @return The timestamp of the last change.
84  	 */
85  	public Date getChanged() {
86  		return _changed;
87  	}
88  	
89  	/**
90  	 * Sets the date where this object was last-changed. The changed-property
91  	 * is intended to be used by Hibernate for providing optimistic locking.
92  	 * @param changed The timestamp of the last change.
93  	 */
94  	public void setChanged(Date changed) {
95  		_changed = changed;
96  	}
97  	
98  	/**
99  	 * Returns the date when this object was inserted (created).
100 	 * @return The creation timestamp.
101 	 */
102 	public Date getInserted() {
103 		return _inserted;
104 	}
105 	
106 	/**
107 	 * Sets the date when this object was inserted (created). The 
108 	 * inserted-property is set automatically in the constructor. There is
109 	 * normally no use for setting this property. 
110 	 * @param inserted The creation timestamp.
111 	 */
112 	public void setInserted(Date inserted) {
113 		_inserted = inserted;
114 	}
115 	
116 }