001 /*
002 * Copyright (C) 2010 eXo Platform SAS.
003 *
004 * This is free software; you can redistribute it and/or modify it
005 * under the terms of the GNU Lesser General Public License as
006 * published by the Free Software Foundation; either version 2.1 of
007 * the License, or (at your option) any later version.
008 *
009 * This software is distributed in the hope that it will be useful,
010 * but WITHOUT ANY WARRANTY; without even the implied warranty of
011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012 * Lesser General Public License for more details.
013 *
014 * You should have received a copy of the GNU Lesser General Public
015 * License along with this software; if not, write to the Free
016 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
017 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
018 */
019
020 package org.crsh.cmdline;
021
022 import org.crsh.cmdline.annotations.Man;
023 import org.crsh.cmdline.annotations.Usage;
024
025 import java.lang.annotation.Annotation;
026 import java.lang.reflect.AnnotatedElement;
027
028 /**
029 * @author <a href="mailto:julien.viet@exoplatform.com">Julien Viet</a>
030 * @version $Revision$
031 */
032 public final class Description {
033
034 /** . */
035 private final String usage;
036
037 /** . */
038 private final String man;
039
040 public Description() {
041 this.usage = this.man = "";
042 }
043
044 Description(Description child, Description parent) {
045 if (child == null) {
046 throw new NullPointerException();
047 }
048 if (parent == null) {
049 throw new NullPointerException();
050 }
051
052 //
053 this.usage = child.usage.length() > 0 ? child.usage : parent.usage;
054 this.man = child.man.length() > 0 ? child.man : parent.man;
055 }
056
057 Description(String usage, String man) {
058 if (usage == null) {
059 throw new NullPointerException();
060 }
061 if (man == null) {
062 throw new NullPointerException();
063 }
064
065 //
066 this.usage = usage;
067 this.man = man;
068 }
069
070 public Description(AnnotatedElement annotated) {
071 this(annotated.getAnnotations());
072 }
073
074 Description(Annotation... annotations) {
075 if (annotations == null) {
076 throw new NullPointerException();
077 }
078
079 //
080 String usage = "";
081 String man = "";
082 for (Annotation annotation : annotations) {
083 if (annotation instanceof Usage) {
084 usage = ((Usage)annotation).value();
085 } else if (annotation instanceof Man) {
086 man = ((Man)annotation).value();
087 }
088 }
089
090 //
091 this.usage = usage;
092 this.man = man;
093 }
094
095 public String getUsage() {
096 return usage;
097 }
098
099 public String getMan() {
100 return man;
101 }
102
103 String getBestEffortMan() {
104 if (man.length() > 0) {
105 return man;
106 } else {
107 return usage;
108 }
109 }
110
111 @Override
112 public boolean equals(Object obj) {
113 if (obj == this) {
114 return true;
115 } else if (obj instanceof Description) {
116 Description that = (Description)obj;
117 return usage.equals(that.usage) && man.equals(that.man);
118 } else {
119 return false;
120 }
121 }
122 }