Example usage for java.lang IndexOutOfBoundsException IndexOutOfBoundsException

List of usage examples for java.lang IndexOutOfBoundsException IndexOutOfBoundsException

Introduction

In this page you can find the example usage for java.lang IndexOutOfBoundsException IndexOutOfBoundsException.

Prototype

public IndexOutOfBoundsException(int index) 

Source Link

Document

Constructs a new IndexOutOfBoundsException class with an argument indicating the illegal index.

Usage

From source file:ListOfLists.java

public Object get(int index) {
    int size = size();
    if (index < 0)
        throw new IndexOutOfBoundsException("index: " + index + "; size: " + size);

    Iterator it = lists.iterator();
    while (it.hasNext()) {
        List list = (List) it.next();
        if (index < list.size()) {
            return list.get(index);
        } else//from  w  w  w.ja  v a 2s  . c o  m
            index -= list.size();
    }

    // if value has not been returned yet - IndexOutOfBoundsException is thrown
    throw new IndexOutOfBoundsException("index: " + index + "; size: " + size);
}

From source file:com.galenframework.page.Rect.java

public Rect(Integer[] numbers) {
    if (numbers.length != 4) {
        throw new IndexOutOfBoundsException("Rect should take 4 arguments, got " + numbers.length);
    }/*www  .  ja  v  a2 s .  co m*/
    this.left = numbers[0];
    this.top = numbers[1];
    this.width = numbers[2];
    this.height = numbers[3];
}

From source file:org.vietspider.server.handler.cms.metas.FilterContentHandler.java

public String handle(final HttpRequest request, final HttpResponse response, final HttpContext context,
        String... params) throws Exception {
    String pattern = params[1];// ww  w.j  ava2s.  c  om
    int idx = pattern.indexOf('=');
    if (idx < 0 || idx >= pattern.length() - 1) {
        throw new IndexOutOfBoundsException("Incorrect parammeter");
    }

    String filter = URLDecoder.decode(pattern.substring(idx + 1), "UTF-8");

    MetaList metas = new MetaList();
    metas.setAction("FILTER");
    metas.setCurrentPage(Integer.parseInt(params[0]));

    File file = UtilFile.getFile("system/plugin/filter", NameConverter.encode(filter));
    if (file.exists() && file.length() > 0) {
        String text = new String(RWData.getInstance().load(file), Application.CHARSET);

        int start = 0;
        while (start < text.length()) {
            char c = text.charAt(start);
            if (Character.isLetterOrDigit(c))
                break;
            start++;
        }

        int end = text.length() - 1;
        while (end > 0) {
            char c = text.charAt(end);
            if (Character.isLetterOrDigit(c))
                break;
            end--;
        }

        StringBuilder builder = new StringBuilder("\"");
        for (int i = start; i <= end; i++) {
            char c = text.charAt(i);
            if (c == ',') {
                builder.append("\" AND \"");
            } else if (c == '\n') {
                builder.append("\" OR \"");
            } else if (Character.isLetterOrDigit(c)) {
                builder.append(c);
            } else if (Character.isSpaceChar(c) || Character.isWhitespace(c)) {
                builder.append(' ');
            }
        }
        builder.append("\"");

        //      System.out.println(" thay co filter "+ builder);

        ArticleSearchQuery query = new ArticleSearchQuery();
        query.setPattern(builder.toString());

        if (Application.LICENSE != Install.PERSONAL) {
            query.setHighlightStart("no");
            ArticleIndexStorage.getInstance().search(metas, query);
        }
    }

    metas.setTitle(filter);
    metas.setUrl("?text=" + pattern.substring(idx + 1));

    return write(request, response, context, metas, params);
}

From source file:com.norman0406.slimgress.API.Player.Agent.java

public int getLevel() {
    // TODO: more efficient?

    for (int i = mLevelAP.length - 1; i >= 0; i--) {
        if (this.getAp() >= mLevelAP[i])
            return i + 1;
    }/*from   w  w  w  .  ja  va2s .c  o  m*/

    throw new IndexOutOfBoundsException("agent level could not be retrieved");
}

From source file:Main.java

/**
 * Returns the <code>index</code>-th value in <code>object</code>, throwing
 * <code>IndexOutOfBoundsException</code> if there is no such element or
 * <code>IllegalArgumentException</code> if <code>object</code> is not an
 * instance of one of the supported types.
 * <p/>//from  w w w .  j a  v  a 2 s  .  c o m
 * The supported types, and associated semantics are:
 * <ul>
 * <li> Map -- the value returned is the <code>Map.Entry</code> in position
 * <code>index</code> in the map's <code>entrySet</code> iterator,
 * if there is such an entry.</li>
 * <li> List -- this method is equivalent to the list's get method.</li>
 * <li> Array -- the <code>index</code>-th array entry is returned,
 * if there is such an entry; otherwise an <code>IndexOutOfBoundsException</code>
 * is thrown.</li>
 * <li> Collection -- the value returned is the <code>index</code>-th object
 * returned by the collection's default iterator, if there is such an element.</li>
 * <li> Iterator or Enumeration -- the value returned is the
 * <code>index</code>-th object in the Iterator/Enumeration, if there
 * is such an element.  The Iterator/Enumeration is advanced to
 * <code>index</code> (or to the end, if <code>index</code> exceeds the
 * number of entries) as a side effect of this method.</li>
 * </ul>
 *
 * @param object the object to get a value from
 * @param index  the index to get
 * @return the object at the specified index
 * @throws IndexOutOfBoundsException if the index is invalid
 * @throws IllegalArgumentException  if the object type is invalid
 */
public static Object get(Object object, int index) {
    if (index < 0)
        throw new IndexOutOfBoundsException("Index cannot be negative: " + index);

    if (object instanceof Map) {
        Map map = (Map) object;
        Iterator iterator = map.entrySet().iterator();
        return get(iterator, index);
    } else if (object instanceof List) {
        return ((List) object).get(index);
    } else if (object instanceof Object[]) {
        return ((Object[]) object)[index];
    } else if (object instanceof Iterator) {
        Iterator it = (Iterator) object;
        while (it.hasNext()) {
            index--;

            if (index == -1)
                return it.next();
            else
                it.next();
        }

        throw new IndexOutOfBoundsException("Entry does not exist: " + index);
    } else if (object instanceof Collection) {
        Iterator iterator = ((Collection) object).iterator();
        return get(iterator, index);
    } else if (object instanceof Enumeration) {
        Enumeration it = (Enumeration) object;
        while (it.hasMoreElements()) {
            index--;

            if (index == -1)
                return it.nextElement();
            else
                it.nextElement();
        }

        throw new IndexOutOfBoundsException("Entry does not exist: " + index);
    } else if (object == null) {
        throw new IllegalArgumentException("Unsupported object type: null");
    } else {
        try {
            return Array.get(object, index);
        } catch (IllegalArgumentException ex) {
            throw new IllegalArgumentException("Unsupported object type: " + object.getClass().getName());
        }
    }
}

From source file:org.vietspider.server.handler.cms.metas.IndexSearchHandler.java

public String handle(final HttpRequest request, final HttpResponse response, final HttpContext context,
        String... params) throws Exception {
    String pattern = params[1];/*from  ww w  .j  av a2 s. c o m*/
    int idx = pattern.indexOf('=');
    if (idx < 0 || idx >= pattern.length() - 1) {
        throw new IndexOutOfBoundsException("Incorrect parammeter");
    }

    String search = URLDecoder.decode(pattern.substring(idx + 1), "UTF-8");

    pattern = search;

    MetaList metas = new MetaList();
    metas.setAction("SEARCH");
    metas.setCurrentPage(Integer.parseInt(params[0]));

    String[][] parameters = ParameterParser.getParameters(params[1]);

    /*SearchQuery query = null;
    if(parameters.length > 0) {
      query = new SearchQuery(parameters[0][1]);
    //      for(int i = 1; i < parameters.length; i++) {
    //        if("region".equalsIgnoreCase(parameters[i][0])) {
    //          query.setRegion(parameters[i][1]);
    //        }
    //      }
    } else {
      throw new IndexOutOfBoundsException("Incorrect parammeter");
    }*/

    ArticleSearchQuery query = new ArticleSearchQuery();
    for (int i = 0; i < parameters.length; i++) {
        if (parameters[i][0].charAt(0) == '?') {
            parameters[i][0] = parameters[i][0].substring(1);
        }
        //      System.out.println(parameters[i][0] + " : "+ parameters[i][1]);
        if (parameters[i][0].equals("text")) {
            query.setPattern(parameters[i][1]);
        } else if (parameters[i][0].equals("source")) {
            query.setSource(parameters[i][1]);
        } else if (parameters[i][0].equals("dtstart")) {
            parameters[i][1] = parameters[i][1].replace('.', '/');
            try {
                SimpleDateFormat dateFormat = CalendarUtils.getDateFormat();
                Date date = dateFormat.parse(parameters[i][1]);
                Calendar calendar = Calendar.getInstance();
                calendar.setTime(date);
                calendar.set(Calendar.HOUR, 0);
                calendar.set(Calendar.MINUTE, 0);
                calendar.set(Calendar.SECOND, 0);
                query.setIndexFrom(calendar.getTimeInMillis());
            } catch (Exception e) {
                LogService.getInstance().setMessage(e, e.toString());
            }
        } else if (parameters[i][0].equals("dtend")) {
            parameters[i][1] = parameters[i][1].replace('.', '/');
            try {
                SimpleDateFormat dateFormat = CalendarUtils.getDateFormat();
                Date date = dateFormat.parse(parameters[i][1]);
                Calendar calendar = Calendar.getInstance();
                calendar.setTime(date);
                calendar.set(Calendar.HOUR, 0);
                calendar.set(Calendar.MINUTE, 0);
                calendar.set(Calendar.SECOND, 0);
                calendar.set(Calendar.DATE, calendar.get(Calendar.DATE) + 1);
                query.setIndexTo(calendar.getTimeInMillis());
            } catch (Exception e) {
                LogService.getInstance().setMessage(e, e.toString());
            }
        }
    }

    if (Application.LICENSE != Install.PERSONAL) {
        //      ArticleSearchQuery query = new ArticleSearchQuery(parameters[0][1]);
        query.setHighlightStart("<b style=\"color: black; background-color: rgb(255, 255, 102);\">");
        query.setHighlightEnd("</b>");
        ArticleIndexStorage.getInstance().search(metas, query);
    }

    //    System.out.println("thay co " + metas.getData().size());

    //    System.out.println(parameters[0][1]);

    //    File tempFolder = UtilFile.getFolder("content/temp/search/");
    //    ContentSearcher2 searcher = new ContentSearcher2(ContentSearcher2.SIMPLE, tempFolder);
    //    HighlightBuilder highlighter = new HighlightBuilder(query.getPattern().toLowerCase());
    //    highlighter.setHighlightTag("<b style=\"color: black; background-color: rgb(255, 255, 102);\">", "</b>");
    //      query.setParser(new QueryParser());
    //    DefaultArticleHandler articleHandler2 = new DefaultArticleHandler(highlighter);
    //    searcher.search(metas, query, articleHandler2);
    //    ContentSearcher searcher = new ContentSearcher();
    //    searcher.search(metas, pattern);

    WebRM resources = new WebRM();
    StringBuilder builder = new StringBuilder(resources.getLabel("search"));
    metas.setTitle(builder.toString());
    metas.setUrl(params[1]);

    return write(request, response, context, metas, params);
}

From source file:HexUtil.java

/**
 * Converts a String of hex characters into an array of bytes.
 * /*from  w ww  . j  a va2  s .c o  m*/
 * @param s
 *            A string of hex characters (upper case or lower) of even
 *            length.
 * @param out
 *            A byte array of length at least s.length()/2 + off
 * @param off
 *            The first byte to write of the array
 */
public static final void hexToBytes(String s, byte[] out, int off)
        throws NumberFormatException, IndexOutOfBoundsException {

    int slen = s.length();
    if ((slen % 2) != 0) {
        s = '0' + s;
    }

    if (out.length < off + slen / 2) {
        throw new IndexOutOfBoundsException(
                "Output buffer too small for input (" + out.length + '<' + off + slen / 2 + ')');
    }

    // Safe to assume the string is even length
    byte b1, b2;
    for (int i = 0; i < slen; i += 2) {
        b1 = (byte) Character.digit(s.charAt(i), 16);
        b2 = (byte) Character.digit(s.charAt(i + 1), 16);
        if ((b1 < 0) || (b2 < 0)) {
            throw new NumberFormatException();
        }
        out[off + i / 2] = (byte) (b1 << 4 | b2);
    }
}

From source file:cc.metapro.openct.splash.InitPagerAdapter.java

@Override
public Fragment getItem(int position) {
    switch (position) {
    case 0://from  www  .  j  a va2 s. c o m
        return mSchoolFragment;
    case 1:
        return mCmsFragment;
    case 2:
        return mLibFragment;
    }
    throw new IndexOutOfBoundsException("three fragments at most!");
}

From source file:DynamicIntArray.java

public int get(int index) {
    if (index >= _length) {
        throw new IndexOutOfBoundsException("Index " + index + " is outside of 0.." + (_length - 1));
    }// w w w.  j  av  a  2 s  . c  o  m
    int i = index / _chunkSize;
    int j = index % _chunkSize;
    if (_data[i] == null) {
        return 0;
    } else {
        return _data[i][j];
    }
}

From source file:uk.ac.tgac.conan.core.service.impl.PdfOperationsServiceImpl.java

@Override
public void extractPage(File in, File out, int page) throws IOException, DocumentException {

    log.debug("Starting PDF page extraction");

    Document document = new Document();

    // Create a reader for the input file
    PdfReader reader = new PdfReader(new FileInputStream(in));

    if (page > reader.getNumberOfPages())
        throw new IndexOutOfBoundsException("Page number " + page + " does not exist in " + in.getPath());

    // Create a copier for the output file
    PdfCopy copy = new PdfCopy(document, new FileOutputStream(out));

    log.debug("PDF extraction resources created");

    document.open();//from ww  w. j a v a2 s.co m

    copy.addPage(copy.getImportedPage(reader, page));

    document.close();

    log.debug("Starting PDF page extracted successfully");
}