root/trunk/src/java/org/jcoderz/guidelines/snippets/SampleSnippets.java

Revision 1011, 8.6 kB (checked in by amandel, 4 years ago)

Aligned svn keyword settings.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
Line 
1// BEGIN SNIPPET: CopyrightHeader.xml
2/*
3 * $Id$
4 *
5 * Copyright 2006, The jCoderZ.org Project. All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions are
9 * met:
10 *
11 *    * Redistributions of source code must retain the above copyright
12 *      notice, this list of conditions and the following disclaimer.
13 *    * Redistributions in binary form must reproduce the above
14 *      copyright notice, this list of conditions and the following
15 *      disclaimer in the documentation and/or other materials
16 *      provided with the distribution.
17 *    * Neither the name of the jCoderZ.org Project nor the names of
18 *      its contributors may be used to endorse or promote products
19 *      derived from this software without specific prior written
20 *      permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS "AS IS" AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
25 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS AND CONTRIBUTORS
26 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
27 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
28 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
29 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
30 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
31 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
32 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 */
34// END SNIPPET
35package org.jcoderz.guidelines.snippets;
36
37
38// BEGIN SNIPPET: SingleTypeImports.xml
39import java.io.IOException;
40import java.io.Serializable;
41// END SNIPPET
42import java.util.ArrayList;
43import java.util.List;
44import java.util.EventObject;
45
46// BEGIN SNIPPET: OnDemandImports.xml
47import java.util.*; // DON'T
48// END SNIPPET
49import java.util.logging.Logger;
50
51
52import javax.naming.Context;
53import javax.servlet.http.HttpServletResponse;
54
55
56/**
57 * Blabla.
58 *
59 * @author mrumpf
60 */
61public class SampleSnippets
62        implements Serializable
63{
64    private static final int DEFAULT_SIZE = 30;
65    private static final int SIZE = 30;
66
67    // utility class
68    private SampleSnippets ()
69    {
70
71    }
72
73    // BEGIN SNIPPET: ExampleClass.xml
74    /**
75     * The Example class provides ...
76     * @author Stephen Mohr
77     * @author Oliver Griffin
78     */
79    public class ExampleClass
80    {
81        // ...
82    }
83    // END SNIPPET
84
85    private static int funtionCallReturn ()
86    {
87        final java.util.ArrayList list = new java.util.ArrayList();
88        int size = list.size();
89     
90        if (size > 0)
91        {   
92            // BEGIN SNIPPET: ReturnStatement.xml
93            return list.size();
94            // PAUSE SNIPPET
95        }
96        else
97        {   
98            // RESUME SNIPPET     
99            // OR
100            return (size != 0 ? size : DEFAULT_SIZE);
101            // END SNIPPET
102        }
103    }
104
105    private static void statements ()
106    {
107        int i = 0;
108
109        // BEGIN SNIPPET: IfStatement.xml
110        if (i > 0)
111        {
112            // ...
113        }
114
115        if (i > 0)
116        {
117            // ...
118        }
119        else
120        {
121            // ...
122        }
123
124        if (i > 0)
125        {
126            // ...
127        }
128        else if (i == 0)
129        {
130            // ...
131        }
132        else
133        {
134            // ...
135        }
136        // END SNIPPET
137
138        // BEGIN SNIPPET: ForStatement.xml
139        for (int j = 0; j < SIZE; j++)
140        {
141            // ...
142        }
143        // END SNIPPET
144
145        // BEGIN SNIPPET: WhileLoop.xml
146        while (i > 0)
147        {
148            // ...
149        }
150        // END SNIPPET
151
152        // FIXME: this should not be allowed!
153        // BEGIN SNIPPET: SimpleWhileLoop.xml
154        while (--i > 0);  // DON'T
155        // END SNIPPET
156
157        // BEGIN SNIPPET: DoWhileLoop.xml
158        do
159        {
160            // ...
161        }
162        while (i > 0);
163        // END SNIPPET
164
165        // BEGIN SNIPPET: Switch.xml
166        switch (i)
167        {
168            case HttpServletResponse.SC_ACCEPTED:
169                // ...
170                /* falls through */
171            case HttpServletResponse.SC_BAD_REQUEST:
172                // ...
173                break;
174            case HttpServletResponse.SC_CONTINUE:
175                // ...
176                break;
177            default:
178                throw new RuntimeException("Unexpected condition.");
179                // no break here because position is unreachable!
180        }
181        // END SNIPPET
182
183        // BEGIN SNIPPET: TryCatchFinally.xml
184        try
185        {
186            // ...
187        }
188        catch (IllegalArgumentException ex)
189        {
190            // ...
191        }
192
193        try
194        {
195            // ...
196        }
197        catch (IllegalArgumentException ex)
198        {
199            // ...
200        }
201        finally
202        {
203            // ...
204        }
205
206        try
207        {
208            // ...
209        }
210        finally
211        {
212            // ...
213        }
214        // END SNIPPET
215    }
216
217    private static boolean aMethod (byte a, Object b)
218    {
219        return true;
220    }
221
222    // BEGIN SNIPPET: MethodDeclarationWhitespace.xml
223    private static void anotherMethod (int a, int b)
224    {
225        // ...
226    }
227    // END SNIPPET
228
229    private static void whitespace ()
230    {
231        int a = 0;
232        int b = 1;
233        int c = -1;
234        // BEGIN SNIPPET: BinaryOperatorWhitespace.xml
235        int d = 1;
236        b = (a + b) / (c * ++d);
237        System.out.println("c=" + c + "\n");
238        // END SNIPPET
239
240        final Integer x = new Integer(b);
241        // BEGIN SNIPPET: Casts.xml
242        final boolean result = aMethod((byte) a, (Object) x);
243        anotherMethod((int) (a + 1), ((int) (b + MAX_LOOPS)) + 1);
244        // END SNIPPET
245    }
246
247    // BEGIN SNIPPET: Constants.xml
248    static final int MIN_WIDTH = 4;
249    static final int MAX_WIDTH = 999;
250    static final int GET_THE_CPU = 1;
251    static final long serialVersionUID = -7064645359225861305L;
252    static final Logger logger
253            = Logger.getLogger(SampleSnippets.class.getName());
254    // END SNIPPET
255
256    private static void naming ()
257    {
258        // BEGIN SNIPPET: VariableNames.xml
259        int i;
260        char c;
261        float myWidth;
262        // END SNIPPET
263    }
264
265    // BEGIN SNIPPET: VariableDeclarations.xml
266    void yetAnotherMethod ()
267    {
268        int int1 = 0; // beginning of method block
269
270        if (int1 == 0)
271        {
272            int int2 = 0; // beginning of "if" block
273
274            // ...
275        }
276    }
277    // END SNIPPET
278
279    private static final int MAX_LOOPS = 100;
280
281    private static void practices ()
282    {
283        // BEGIN SNIPPET: VariableDeclarationsException.xml
284        for (int i = 0; i < MAX_LOOPS; i++)
285        {
286            // ...
287        }
288        // END SNIPPET
289    }
290
291    // BEGIN SNIPPET: HideGlobalMember.xml
292    int count; // should be mCount anyway!
293
294    void counter ()
295    {
296        if (count > 0)
297        {
298            int count;  // DON'T
299
300            // ...
301        }
302
303        // ...
304    }
305    // END SNIPPET
306
307    // BEGIN SNIPPET: InterfaceServiceDeclaration.xml
308    public interface ActionListener
309    {
310        void actionPerformed (EventObject event);
311    }
312    // END SNIPPET
313
314    // BEGIN SNIPPET: InterfaceCapabilitiesDeclaration.xml
315    public interface Runnable
316    {
317        void run ();
318    }
319
320    public interface Accessible
321    {
322        Context getContext ();
323    }
324    // END SNIPPET
325
326    public static final int MAX_CUSTOMERS = 100;
327
328    // BEGIN SNIPPET: PluralizeSample.xml
329    private Object[] mCustomers = new Object[MAX_CUSTOMERS];
330
331    void addCustomer (int index, Object customer)
332    {
333        mCustomers[index] = customer;
334    }
335    // END SNIPPET
336
337    // BEGIN SNIPPET: IndentationExtendsAndImplements.xml
338    public class IndentionSample
339        extends SampleSnippets
340        implements Serializable, Cloneable, Comparable
341    {   
342        /**
343         *
344         */
345        public void doSomething (int length)
346            throws IOException
347        // ...
348    // END SNIPPET
349        {
350            final List servers = new ArrayList();
351            /*
352            // BEGIN SNIPPET: IndentationSampleExtended.xml
353            final SimpleBusinessResultException e
354                    = new SimpleBusinessResultException(
355                        ResultCode.SPLIT_AUTHORIZATION_SPLIT_INDEX_UNEXPECTED);
356             // END SNIPPET
357             */
358        }
359
360        /** {@inheritDoc} */
361        public int compareTo (Object o)
362        {
363            // TODO Auto-generated method stub
364            return 0;
365        }
366    }
367
368}
Note: See TracBrowser for help on using the browser.