Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2006, 2012, 2011 IBM Corporation, Zeligsoft Inc., and others.
 * All rights reserved.   This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *   IBM - Initial API and implementation
 *   Zeligsoft - Bugs 244946, 248869
 *   Axel Uhl (SAP AG) - Bug 342644
 *******************************************************************************/

import java.util.Collection;

import java.util.Iterator;

public class Main {
    /**
     * Implementation of the OCL
     * <ul>
     * <li><tt>OrderedSet::at(index : Integer) : T</tt></li>
     * <li><tt>Sequence::at(index : Integer) : T</tt></li>
     * </ul>
     * operations.
     * 
     * @param self the source collection
     * @param index the 1-based (in OCL fashion) index
     * @return the object at the specified index of the source collection
     * 
     * @throws IndexOutOfBoundsException if the index is out of bounds
     */
    public static <E> E at(Collection<E> self, int index) {
        index = index - 1;

        if (index < 0 || index >= self.size()) {
            throw new IndexOutOfBoundsException("index: " + (index + 1) + ", size: " //$NON-NLS-1$ //$NON-NLS-2$
                    + self.size());
        }

        int curr = 0;
        for (Iterator<E> it = self.iterator(); it.hasNext();) {
            E object = it.next();
            if (curr++ == index) {
                return object;
            }
        }
        return null; // undefined
    }
}