| 1 | /* |
|---|
| 2 | * fmgVen - A Convention over Configuration Java ORM Tool |
|---|
| 3 | * Copyright 2011 Fatih Mehmet Güler |
|---|
| 4 | * |
|---|
| 5 | * Licensed under the Apache License, Version 2.0 (the "License"); |
|---|
| 6 | * you may not use this file except in compliance with the License. |
|---|
| 7 | * You may obtain a copy of the License at |
|---|
| 8 | * |
|---|
| 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
|---|
| 10 | * |
|---|
| 11 | * Unless required by applicable law or agreed to in writing, software |
|---|
| 12 | * distributed under the License is distributed on an "AS IS" BASIS, |
|---|
| 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|---|
| 14 | * See the License for the specific language governing permissions and |
|---|
| 15 | * limitations under the License. |
|---|
| 16 | * under the License. |
|---|
| 17 | */ |
|---|
| 18 | package com.fmguler.ven.util; |
|---|
| 19 | |
|---|
| 20 | import java.util.LinkedList; |
|---|
| 21 | import java.util.List; |
|---|
| 22 | |
|---|
| 23 | /** |
|---|
| 24 | * A Simple Linked List which informs the type of its elements. |
|---|
| 25 | * @author Fatih Mehmet Güler |
|---|
| 26 | */ |
|---|
| 27 | public class VenList extends LinkedList { |
|---|
| 28 | private Class elementClass; |
|---|
| 29 | private String joinField; |
|---|
| 30 | |
|---|
| 31 | /** |
|---|
| 32 | * Creates new instance of VenList |
|---|
| 33 | */ |
|---|
| 34 | public VenList() { |
|---|
| 35 | } |
|---|
| 36 | |
|---|
| 37 | /** |
|---|
| 38 | * Creates new instance of VenList, specifying the type of elements |
|---|
| 39 | * @param elementClass the type of elements of this list |
|---|
| 40 | */ |
|---|
| 41 | public VenList(Class elementClass) { |
|---|
| 42 | this.elementClass = elementClass; |
|---|
| 43 | } |
|---|
| 44 | |
|---|
| 45 | /** |
|---|
| 46 | * Creates new instance of VenList, specifying the type of elements and the join field |
|---|
| 47 | * @param elementClass the type of elements of this list |
|---|
| 48 | * @param joinField the field which refers to the parent object |
|---|
| 49 | */ |
|---|
| 50 | public VenList(Class elementClass, String joinField) { |
|---|
| 51 | this.elementClass = elementClass; |
|---|
| 52 | this.joinField = joinField; |
|---|
| 53 | } |
|---|
| 54 | |
|---|
| 55 | /** |
|---|
| 56 | * Returns the type of the elements in this list. |
|---|
| 57 | * @return element type |
|---|
| 58 | */ |
|---|
| 59 | public Class getElementClass() { |
|---|
| 60 | return elementClass; |
|---|
| 61 | } |
|---|
| 62 | |
|---|
| 63 | /** |
|---|
| 64 | * Returns the field in the element class which refers the parent object |
|---|
| 65 | * @return element join field |
|---|
| 66 | */ |
|---|
| 67 | public String getJoinField() { |
|---|
| 68 | return joinField; |
|---|
| 69 | } |
|---|
| 70 | |
|---|
| 71 | /** |
|---|
| 72 | * Return the class of the elements in the list |
|---|
| 73 | * @param list the list |
|---|
| 74 | * @return the element class in the list |
|---|
| 75 | */ |
|---|
| 76 | public static Class findElementClass(List list) { |
|---|
| 77 | if (list.size() > 0) { |
|---|
| 78 | Object elem = list.get(0); |
|---|
| 79 | return elem.getClass(); |
|---|
| 80 | } |
|---|
| 81 | if (list instanceof VenList) { |
|---|
| 82 | return ((VenList)list).getElementClass(); |
|---|
| 83 | } else { |
|---|
| 84 | //find according to 1.5 generic or some convention (e.g. xyzList -> xyz) |
|---|
| 85 | return null; |
|---|
| 86 | } |
|---|
| 87 | } |
|---|
| 88 | } |
|---|