View Javadoc
1   /*
2   * Copyright 2012-2025 Christophe Friederich
3   *
4   * Licensed under the Apache License, Version 2.0 (the "License");
5   * you may not use this file except in compliance with the License.
6   * You may obtain a copy of the License at
7   *
8   * http://www.apache.org/licenses/LICENSE-2.0
9   *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16  package org.devacfr.maven.skins.reflow.snippet;
17  
18  import java.util.ArrayList;
19  import org.jsoup.internal.StringUtil;
20  
21  /**
22   * @author Christophe Friederich
23   * @version 2.4
24   */
25  public class Components extends ArrayList<Component<?>> {
26  
27    /**
28     * @see java.io.Serializable
29     */
30    private static final long serialVersionUID = 1L;
31  
32    /**
33     * @see Components#empty()
34     */
35    private static final Components EMPTY = new Components();
36  
37    /**
38     * @return an empty Components instance
39     */
40    public static Components empty() {
41      return EMPTY;
42    }
43  
44    /**
45     * Default constructor.
46     */
47    public Components() {
48    }
49  
50    /**
51     * @return the HTML representation of all components
52     */
53    public String html() {
54      final StringBuilder sb = StringUtil.borrowBuilder();
55      for (final Component<?> component : this) {
56        if (sb.length() != 0) {
57          sb.append("\n");
58        }
59        final String html = component.getHtml();
60        if (html != null) {
61          sb.append(html);
62        }
63      }
64      return StringUtil.releaseBuilder(sb);
65    }
66  
67    /**
68     * Get the first matched element.
69     *
70     * @return The first matched component, or <code>null</code> if contents is empty.
71     */
72    public Component<?> first() {
73      return isEmpty() ? null : get(0);
74    }
75  
76    /**
77     * Get the last matched component.
78     *
79     * @return The last matched component, or <code>null</code> if contents is empty.
80     */
81    public Component<?> last() {
82      return isEmpty() ? null : get(size() - 1);
83    }
84  }