Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 1 | /** |
| 2 | * Copyright 2011, Big Switch Networks, Inc. |
| 3 | * Originally created by David Erickson, Stanford University |
| 4 | * |
| 5 | * Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 6 | * not use this file except in compliance with the License. You may obtain |
| 7 | * 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, WITHOUT |
| 13 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 14 | * License for the specific language governing permissions and limitations |
| 15 | * under the License. |
| 16 | **/ |
| 17 | |
| 18 | package net.floodlightcontroller.storage; |
| 19 | |
| 20 | import java.util.ArrayList; |
| 21 | import java.util.List; |
| 22 | |
| 23 | public class RowOrdering { |
| 24 | |
| 25 | public enum Direction { ASCENDING, DESCENDING }; |
| 26 | |
| 27 | public class Item { |
| 28 | |
| 29 | private String column; |
| 30 | private Direction direction; |
| 31 | |
| 32 | public Item(String column, Direction direction) { |
| 33 | assert(column != null); |
| 34 | assert(direction != null); |
| 35 | this.column = column; |
| 36 | this.direction = direction; |
| 37 | } |
| 38 | |
| 39 | public String getColumn() { |
| 40 | return column; |
| 41 | } |
| 42 | |
| 43 | public Direction getDirection() { |
| 44 | return direction; |
| 45 | } |
| 46 | } |
| 47 | |
| 48 | private List<Item> itemList = new ArrayList<Item>(); |
| 49 | |
| 50 | public RowOrdering() { |
| 51 | } |
| 52 | |
| 53 | public RowOrdering(String column) { |
| 54 | add(column); |
| 55 | } |
| 56 | |
| 57 | public RowOrdering(String column, Direction direction) { |
| 58 | add(column, direction); |
| 59 | } |
| 60 | |
| 61 | public RowOrdering(Item item) { |
| 62 | add(item); |
| 63 | } |
| 64 | |
| 65 | public RowOrdering(Item[] itemArray) { |
| 66 | add(itemArray); |
| 67 | } |
| 68 | |
| 69 | public RowOrdering(List<Item> itemList) { |
| 70 | add(itemList); |
| 71 | } |
| 72 | |
| 73 | public void add(String column) { |
| 74 | itemList.add(new Item(column, Direction.ASCENDING)); |
| 75 | } |
| 76 | |
| 77 | public void add(String column, Direction direction) { |
| 78 | itemList.add(new Item(column, direction)); |
| 79 | } |
| 80 | |
| 81 | public void add(Item item) { |
| 82 | assert(item != null); |
| 83 | itemList.add(item); |
| 84 | } |
| 85 | |
| 86 | public void add(Item[] itemArray) { |
| 87 | for (Item item: itemArray) { |
| 88 | itemList.add(item); |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | public void add(List<Item> itemList) { |
| 93 | this.itemList.addAll(itemList); |
| 94 | } |
| 95 | |
| 96 | public List<Item> getItemList() { |
| 97 | return itemList; |
| 98 | } |
| 99 | |
| 100 | public boolean equals(RowOrdering rowOrdering) { |
| 101 | if (rowOrdering == null) |
| 102 | return false; |
| 103 | |
| 104 | int len1 = itemList.size(); |
| 105 | int len2 = rowOrdering.getItemList().size(); |
| 106 | if (len1 != len2) |
| 107 | return false; |
| 108 | |
| 109 | for (int i = 0; i < len1; i++) { |
| 110 | Item item1 = itemList.get(i); |
| 111 | Item item2 = rowOrdering.getItemList().get(i); |
| 112 | if (!item1.getColumn().equals(item2.getColumn()) || |
| 113 | item1.getDirection() != item2.getDirection()) |
| 114 | return false; |
| 115 | } |
| 116 | |
| 117 | return true; |
| 118 | } |
| 119 | } |