|
Groovy example source code file (DefaultGroovyMethodsSupport.java)
The Groovy DefaultGroovyMethodsSupport.java source code
/*
* Copyright 2003-2008 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.codehaus.groovy.runtime;
import groovy.lang.EmptyRange;
import groovy.lang.Range;
import org.codehaus.groovy.runtime.typehandling.DefaultTypeTransformation;
import java.io.Closeable;
import java.io.IOException;
import java.util.*;
import java.util.logging.Logger;
/**
* Support methods for DefaultGroovyMethods and PluginDefaultMethods.
*/
public class DefaultGroovyMethodsSupport {
private static final Logger LOG = Logger.getLogger(DefaultGroovyMethodsSupport.class.getName());
// helper method for getAt and putAt
protected static RangeInfo subListBorders(int size, Range range) {
int from = normaliseIndex(DefaultTypeTransformation.intUnbox(range.getFrom()), size);
int to = normaliseIndex(DefaultTypeTransformation.intUnbox(range.getTo()), size);
boolean reverse = range.isReverse();
if (from > to) {
// support list[1..-1]
int tmp = to;
to = from;
from = tmp;
reverse = !reverse;
}
return new RangeInfo(from, to + 1, reverse);
}
// helper method for getAt and putAt
protected static RangeInfo subListBorders(int size, EmptyRange range) {
int from = normaliseIndex(DefaultTypeTransformation.intUnbox(range.getFrom()), size);
return new RangeInfo(from, from, false);
}
/**
* This converts a possibly negative index to a real index into the array.
*
* @param i the unnormalised index
* @param size the array size
* @return the normalised index
*/
protected static int normaliseIndex(int i, int size) {
int temp = i;
if (i < 0) {
i += size;
}
if (i < 0) {
throw new ArrayIndexOutOfBoundsException("Negative array index [" + temp + "] too large for array size " + size);
}
return i;
}
/**
* Close the Closeable. Logging a warning if any problems occur.
*
* @param c the thing to close
*/
public static void closeWithWarning(Closeable c) {
if (c != null) {
try {
c.close();
} catch (IOException e) {
LOG.warning("Caught exception during close(): " + e);
}
}
}
/**
* Close the Closeable. Ignore any problems that might occur.
*
* @param c the thing to close
*/
public static void closeQuietly(Closeable c) {
if (c != null) {
try {
c.close();
} catch (IOException e) {
/* ignore */
}
}
}
protected static class RangeInfo {
public final int from;
public final int to;
public final boolean reverse;
public RangeInfo(int from, int to, boolean reverse) {
this.from = from;
this.to = to;
this.reverse = reverse;
}
}
protected static <T> Collection
Other Groovy examples (source code examples)Here is a short list of links related to this Groovy DefaultGroovyMethodsSupport.java source code file: |
| ... this post is sponsored by my books ... | |
#1 New Release! |
FP Best Seller |
Copyright 1998-2024 Alvin Alexander, alvinalexander.com
All Rights Reserved.
A percentage of advertising revenue from
pages under the /java/jwarehouse
URI on this website is
paid back to open source projects.