View Javadoc

1   /*
2    * Copyright 2007-2008 the original author or authors.
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  
17  package org.gwe.p2elv2.functions;
18  
19  import java.util.List;
20  import java.util.UUID;
21  
22  import org.gwe.p2elv2.PFunction;
23  import org.gwe.p2elv2.PStatementContext;
24  import org.gwe.p2elv2.PVarValueSpace;
25  
26  /**
27   * @author Marco Ruiz
28   * @since Aug 4, 2008
29   */
30  public class PFUUID extends PFunction {
31  	
32      public static final String FUNCTION_NAME = "uuid";
33  
34  	public PFUUID() { super(FUNCTION_NAME); }
35      
36      public PVarValueSpace calculateValues(List<String> params, PStatementContext ctx) {
37      	int quantity = 1;
38      	if (params.size() > 0) {
39      		try {
40      			quantity = Integer.parseInt(params.get(0));
41      		} catch (Exception e) {}
42      	}
43      	
44      	PVarValueSpace result = new PVarValueSpace();
45  		for (int count = 0; count < quantity; count++) result.add(generateUUID());
46  		return result;
47      }
48  
49  	private String generateUUID() {
50  	    return UUID.randomUUID().toString();
51      }
52  	
53  	public static void main(String[] args) {
54  		PFUUID function = new PFUUID();
55  		System.out.println(function.generateUUID());
56  		System.out.println(function.generateUUID());
57  		System.out.println(function.generateUUID());
58  		System.out.println(function.generateUUID());
59  	}
60  }