📄 bmutil.java
字号:
/* Sesame - Storage and Querying architecture for RDF and RDF Schema * Copyright (C) 2001-2005 Aduna * * Contact: * Aduna * Prinses Julianaplein 14 b * 3817 CS Amersfoort * The Netherlands * tel. +33 (0)33 465 99 87 * fax. +33 (0)33 465 99 87 * * http://aduna.biz/ * http://www.openrdf.org/ * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2.1 of the License, or (at your option) any later version. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this library; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */package org.openrdf.sesame.sail.test.benchmark.util;import java.util.ArrayList;import java.util.Collection;import java.util.Iterator;import java.util.List;import org.openrdf.model.BNode;import org.openrdf.model.Literal;import org.openrdf.model.Resource;import org.openrdf.model.URI;import org.openrdf.model.Value;import org.openrdf.model.impl.BNodeImpl;import org.openrdf.model.impl.LiteralImpl;import org.openrdf.model.impl.URIImpl;import org.openrdf.sesame.sail.test.benchmark.model.BMStatement;import org.openrdf.sesame.sail.test.benchmark.model.Binary;/** * Utility class. * * @author Peter van 't Hof. * @version %I%, %G% */public class BMUtil { /** * Prints notification. * * @param notification Notification to print. */ public static void notify(String notification) { System.out.println("[NOTIFY ] " + notification); } /** * Prints status. * * @param status Status to print. */ public static void status(String status) { System.out.println("[STATUS ] " + status); } /** * Prints warning. * * @param warning Warning to print. */ public static void warning(String warning) { System.out.println("[WARNING ] " + warning); } /** * Prints error message. * * @param error Error message to print. */ public static void error(String error) { System.out.println("[ERROR ] " + error); } /** * Prints message Exception and exits utility. * * @param exception Exception which causes utility to exit. * public static void exit(Exception exception) { error(exception.getMessage() + ". Exiting test"); System.exit(0); } */ /** * Prints error message and exits utility. * * @param error Error message to print. */ public static void exit(String error) { error(error + " Exiting test"); System.exit(0); } /** * Cuts List down to preferred size. Elements in List are distributed evenly * over new List. * * @param list List to cut down. * @param preferredSize Preferred size of new List. * @return Cut down List. */ public static List cutDown(List list, int preferredSize) { return distribute(list, preferredSize); } /** * Distributes elements in Collection evenly over new List with size is equal * to preferred size. * * @param collection Collection to distribute. * @param preferredSize Preferred size of new List. * @return Distributed List. */ public static List distribute(Collection collection, int preferredSize) { // Compute interval on which elements are retrieved. int size = collection.size(); double interval = (double)size/preferredSize; /* If interval <= 1 then distributed is equal to collection. Else * elements are distributed evenly. */ if (interval <= 1) { return stripIds(new ArrayList(collection)); } else { List distributed = new ArrayList(); Iterator i = collection.iterator(); int position = 0; while (i.hasNext()) { position++; /* Computation of last position % interval should be zero and * therefore last element iterator returns should be added to * distributed. Because of possible loss of precision, * computation could fail and last element will not be added to * distributed. To avoid this, another check is made, i.e. is * current position equal to last position. */ if (position % interval < 1 || position == size) { // Position on interval or last value. distributed.add(stripId(i.next())); } else { i.next(); } } return distributed; } } /** * Strips id of Value, if any. * * @param value Value to strip id of. * @return Stripped Value. */ public static Value stripId(Value value) { if (value instanceof Resource) { return stripId((Resource)value); } else { return stripId((Literal)value); } } /** * Strips id of Resource, if any. * * @param resource Resource to strip id of. * @return Stripped resource. */ public static Resource stripId(Resource resource) { if (resource instanceof URI) { return stripId((URI)resource); } else { return stripId((BNode)resource); } } /** * Strips id of URI, if any. * * @param uri URI to strip id of. * @return Stripped URI. */ public static URI stripId(URI uri) { // Strip resource. return new URIImpl(uri.getNamespace(), uri.getLocalName()); } /** * Strips id of BNode, if any. * * @param bNode BNode to strip id of. * @return Stripped BNode. */ public static BNode stripId(BNode bNode) { // Strip resource. return new BNodeImpl(bNode.getID()); } /** * Strips id of Literal, if any. * * @param literal Literal to strip id of. * @return Stripped literal. */ public static Literal stripId(Literal literal) { // Strip literal. URI datatype = literal.getDatatype(); if (datatype != null) { datatype = stripId(datatype); return new LiteralImpl(literal.getLabel(), datatype); } else { return new LiteralImpl(literal.getLabel(), literal.getLanguage()); } } /** * Strips ids of elements in list, if any. * * @param list List containing elements to strip id of. * @return List containing stripped elements. */ public static List stripIds(List list) { List newList = new ArrayList(); Iterator i = list.iterator(); while (i.hasNext()) { newList.add(stripId(i.next())); } return newList; } /** * Strips id of object, if any. If object is an instance of BMStatement, * strips ids of subject, predicate and object, if any. If object is an * instance of Binary, strips ids of left and right, if any. Else if object * is an instance of Value, strips id of Value. * * @param object Object to strip id of. * @return Stripped object, */ public static Object stripId(Object object) { if (object instanceof BMStatement) { BMStatement statement = (BMStatement)object; Resource subject = statement.getSubject(); URI predicate = statement.getPredicate(); Value statementObject = statement.getObject(); if (subject != null) { subject = stripId(subject); } if (predicate != null) { predicate = stripId(predicate); } if (statementObject != null) { statementObject = stripId(statementObject); } return new BMStatement(subject, predicate, statementObject); } else if (object instanceof Binary) { Binary binary = (Binary)object; return new Binary(stripId(binary.getLeft()), stripId(binary.getRight())); } else if (object instanceof Value) { return stripId((Value)object); } return object; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -