1 /* 2 * Copyright (C) 2010 The Android Open Source Project 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 com.android.gallery3d.util; 18 19 20 public class LinkedNode { 21 private LinkedNode mPrev; 22 private LinkedNode mNext; 23 24 public LinkedNode() { 25 mPrev = mNext = this; 26 } 27 28 public void insert(LinkedNode node) { 29 node.mNext = mNext; 30 mNext.mPrev = node; 31 node.mPrev = this; 32 mNext = node; 33 } 34 35 public void remove() { 36 if (mNext == this) throw new IllegalStateException(); 37 mPrev.mNext = mNext; 38 mNext.mPrev = mPrev; 39 mPrev = mNext = null; 40 } 41 42 @SuppressWarnings("unchecked") 43 public static class List<T extends LinkedNode> { 44 private LinkedNode mHead = new LinkedNode(); 45 46 public void insertFirst(T node) { 47 mHead.insert(node); 48 } 49 50 public void insertLast(T node) { 51 mHead.mPrev.insert(node); 52 } 53 54 public T getFirst() { 55 return (T) (mHead.mNext == mHead ? null : mHead.mNext); 56 } 57 58 public T getLast() { 59 return (T) (mHead.mPrev == mHead ? null : mHead.mPrev); 60 } 61 62 public T nextOf(T node) { 63 return (T) (node.mNext == mHead ? null : node.mNext); 64 } 65 66 public T previousOf(T node) { 67 return (T) (node.mPrev == mHead ? null : node.mPrev); 68 } 69 70 } 71 72 public static <T extends LinkedNode> List<T> newList() { 73 return new List<T>(); 74 } 75 } 76