code
stringlengths 3
1.04M
| repo_name
stringlengths 5
109
| path
stringlengths 6
306
| language
stringclasses 1
value | license
stringclasses 15
values | size
int64 3
1.04M
|
---|---|---|---|---|---|
/*
* Copyright © 2014 Cask Data, Inc.
*
* 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 co.cask.cdap.cli.completer.element;
import co.cask.cdap.api.service.http.ServiceHttpEndpoint;
import co.cask.cdap.cli.CLIConfig;
import co.cask.cdap.cli.ProgramIdArgument;
import co.cask.cdap.cli.util.ArgumentParser;
import co.cask.cdap.client.ServiceClient;
import co.cask.cdap.common.NotFoundException;
import co.cask.cdap.common.UnauthorizedException;
import co.cask.cdap.proto.Id;
import co.cask.common.cli.completers.PrefixCompleter;
import com.google.common.collect.Lists;
import java.io.IOException;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Map;
/**
* Prefix completer for Http methods.
*/
public class HttpMethodPrefixCompleter extends PrefixCompleter {
private static final String PROGRAM_ID = "programId";
private static final String PATTERN = String.format("call service <%s>", PROGRAM_ID);
private final ServiceClient serviceClient;
private final EndpointCompleter completer;
private final CLIConfig cliConfig;
public HttpMethodPrefixCompleter(final ServiceClient serviceClient, final CLIConfig cliConfig,
String prefix, EndpointCompleter completer) {
super(prefix, completer);
this.cliConfig = cliConfig;
this.serviceClient = serviceClient;
this.completer = completer;
}
@Override
public int complete(String buffer, int cursor, List<CharSequence> candidates) {
Map<String, String> arguments = ArgumentParser.getArguments(buffer, PATTERN);
ProgramIdArgument programIdArgument = ArgumentParser.parseProgramId(arguments.get(PROGRAM_ID));
if (programIdArgument != null) {
Id.Service service = Id.Service.from(cliConfig.getCurrentNamespace(),
programIdArgument.getAppId(), programIdArgument.getProgramId());
completer.setEndpoints(getMethods(service));
} else {
completer.setEndpoints(Collections.<String>emptyList());
}
return super.complete(buffer, cursor, candidates);
}
public Collection<String> getMethods(Id.Service serviceId) {
Collection<String> httpMethods = Lists.newArrayList();
try {
for (ServiceHttpEndpoint endpoint : serviceClient.getEndpoints(serviceId)) {
String method = endpoint.getMethod();
if (!httpMethods.contains(method)) {
httpMethods.add(method);
}
}
} catch (IOException | UnauthorizedException | NotFoundException ignored) {
}
return httpMethods;
}
}
| chtyim/cdap | cdap-cli/src/main/java/co/cask/cdap/cli/completer/element/HttpMethodPrefixCompleter.java | Java | apache-2.0 | 3,087 |
/*
* Copyright 2000-2013 JetBrains s.r.o.
*
* 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.jetbrains.plugins.javaFX.fxml.refs;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.util.Computable;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.psi.*;
import com.intellij.psi.search.GlobalSearchScope;
import com.intellij.psi.search.LocalSearchScope;
import com.intellij.psi.search.SearchScope;
import com.intellij.psi.search.searches.ReferencesSearch;
import com.intellij.psi.util.PsiUtilCore;
import com.intellij.psi.xml.XmlAttribute;
import com.intellij.psi.xml.XmlAttributeValue;
import com.intellij.util.Processor;
import com.intellij.util.QueryExecutor;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.plugins.javaFX.fxml.FxmlConstants;
import org.jetbrains.plugins.javaFX.indexing.JavaFxControllerClassIndex;
import java.util.List;
/**
* User: anna
* Date: 3/29/13
*/
public class JavaFxControllerFieldSearcher implements QueryExecutor<PsiReference, ReferencesSearch.SearchParameters>{
@Override
public boolean execute(@NotNull final ReferencesSearch.SearchParameters queryParameters, @NotNull final Processor<PsiReference> consumer) {
final PsiElement elementToSearch = queryParameters.getElementToSearch();
if (elementToSearch instanceof PsiField) {
final PsiField field = (PsiField)elementToSearch;
final PsiClass containingClass = ApplicationManager.getApplication().runReadAction(new Computable<PsiClass>() {
@Override
public PsiClass compute() {
return field.getContainingClass();
}
});
if (containingClass != null) {
final String qualifiedName = ApplicationManager.getApplication().runReadAction(new Computable<String>() {
@Override
public String compute() {
return containingClass.getQualifiedName();
}
});
if (qualifiedName != null) {
Project project = PsiUtilCore.getProjectInReadAction(containingClass);
final List<PsiFile> fxmlWithController =
JavaFxControllerClassIndex.findFxmlWithController(project, qualifiedName);
for (final PsiFile file : fxmlWithController) {
ApplicationManager.getApplication().runReadAction(() -> {
final String fieldName = field.getName();
if (fieldName == null) return;
final VirtualFile virtualFile = file.getViewProvider().getVirtualFile();
final SearchScope searchScope = queryParameters.getEffectiveSearchScope();
boolean contains = searchScope instanceof LocalSearchScope ? ((LocalSearchScope)searchScope).isInScope(virtualFile) :
((GlobalSearchScope)searchScope).contains(virtualFile);
if (contains) {
file.accept(new XmlRecursiveElementVisitor() {
@Override
public void visitXmlAttributeValue(final XmlAttributeValue value) {
final PsiReference reference = value.getReference();
if (reference != null) {
final PsiElement resolve = reference.resolve();
if (resolve instanceof XmlAttributeValue) {
final PsiElement parent = resolve.getParent();
if (parent instanceof XmlAttribute) {
final XmlAttribute attribute = (XmlAttribute)parent;
if (FxmlConstants.FX_ID.equals(attribute.getName()) && fieldName.equals(attribute.getValue())) {
consumer.process(reference);
}
}
}
}
}
});
}
});
}
}
}
}
return true;
}
}
| hurricup/intellij-community | plugins/javaFX/src/org/jetbrains/plugins/javaFX/fxml/refs/JavaFxControllerFieldSearcher.java | Java | apache-2.0 | 4,457 |
/*
* Copyright (c) 2008-2018, Hazelcast, Inc. All Rights Reserved.
*
* 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 com.hazelcast.map.impl.querycache.subscriber;
import com.hazelcast.config.Config;
import com.hazelcast.config.MapConfig;
import com.hazelcast.config.QueryCacheConfig;
import com.hazelcast.internal.config.ConfigUtils;
import com.hazelcast.map.impl.querycache.QueryCacheConfigurator;
import com.hazelcast.map.impl.querycache.QueryCacheEventService;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
/**
* Node side implementation of {@link QueryCacheConfigurator}.
*
* @see QueryCacheConfigurator
*/
public class NodeQueryCacheConfigurator extends AbstractQueryCacheConfigurator {
private final Config config;
public NodeQueryCacheConfigurator(Config config, ClassLoader configClassLoader,
QueryCacheEventService eventService) {
super(configClassLoader, eventService);
this.config = config;
}
@Override
public QueryCacheConfig getOrCreateConfiguration(String mapName, String cacheName, String cacheId) {
MapConfig mapConfig = config.getMapConfig(mapName);
QueryCacheConfig queryCacheConfig = findQueryCacheConfigFromMapConfig(mapConfig, cacheName);
if (queryCacheConfig != null) {
setPredicateImpl(queryCacheConfig);
setEntryListener(mapName, cacheId, queryCacheConfig);
return queryCacheConfig;
}
QueryCacheConfig newConfig = new QueryCacheConfig(cacheName);
mapConfig.getQueryCacheConfigs().add(newConfig);
return newConfig;
}
@Override
public QueryCacheConfig getOrNull(String mapName, String cacheName, String cacheId) {
MapConfig mapConfig = config.getMapConfigOrNull(mapName);
if (mapConfig == null) {
return null;
}
QueryCacheConfig queryCacheConfig = findQueryCacheConfigFromMapConfig(mapConfig, cacheName);
if (queryCacheConfig != null) {
setPredicateImpl(queryCacheConfig);
setEntryListener(mapName, cacheId, queryCacheConfig);
return queryCacheConfig;
}
return queryCacheConfig;
}
private QueryCacheConfig findQueryCacheConfigFromMapConfig(MapConfig mapConfig, String cacheName) {
List<QueryCacheConfig> queryCacheConfigs = mapConfig.getQueryCacheConfigs();
Map<String, QueryCacheConfig> allQueryCacheConfigs = new HashMap<String, QueryCacheConfig>(queryCacheConfigs.size());
for (QueryCacheConfig queryCacheConfig : queryCacheConfigs) {
allQueryCacheConfigs.put(queryCacheConfig.getName(), queryCacheConfig);
}
return ConfigUtils.lookupByPattern(config.getConfigPatternMatcher(), allQueryCacheConfigs, cacheName);
}
@Override
public void removeConfiguration(String mapName, String cacheName) {
MapConfig mapConfig = config.getMapConfig(mapName);
List<QueryCacheConfig> queryCacheConfigs = mapConfig.getQueryCacheConfigs();
if (queryCacheConfigs == null || queryCacheConfigs.isEmpty()) {
return;
}
Iterator<QueryCacheConfig> iterator = queryCacheConfigs.iterator();
while (iterator.hasNext()) {
QueryCacheConfig config = iterator.next();
if (config.getName().equals(cacheName)) {
iterator.remove();
}
}
}
}
| Donnerbart/hazelcast | hazelcast/src/main/java/com/hazelcast/map/impl/querycache/subscriber/NodeQueryCacheConfigurator.java | Java | apache-2.0 | 3,996 |
/**
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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.apache.camel.component.twitter.springboot;
import org.springframework.boot.context.properties.ConfigurationProperties;
/**
* This component integrates with Twitter to send tweets or search for tweets
* and more.
*
* Generated by camel-package-maven-plugin - do not edit this file!
*/
@ConfigurationProperties(prefix = "camel.component.twitter")
public class TwitterComponentConfiguration {
/**
* The access token
*/
private String accessToken;
/**
* The access token secret
*/
private String accessTokenSecret;
/**
* The consumer key
*/
private String consumerKey;
/**
* The consumer secret
*/
private String consumerSecret;
/**
* The http proxy host which can be used for the camel-twitter.
*/
private String httpProxyHost;
/**
* The http proxy user which can be used for the camel-twitter.
*/
private String httpProxyUser;
/**
* The http proxy password which can be used for the camel-twitter.
*/
private String httpProxyPassword;
/**
* The http proxy port which can be used for the camel-twitter.
*/
private int httpProxyPort;
public String getAccessToken() {
return accessToken;
}
public void setAccessToken(String accessToken) {
this.accessToken = accessToken;
}
public String getAccessTokenSecret() {
return accessTokenSecret;
}
public void setAccessTokenSecret(String accessTokenSecret) {
this.accessTokenSecret = accessTokenSecret;
}
public String getConsumerKey() {
return consumerKey;
}
public void setConsumerKey(String consumerKey) {
this.consumerKey = consumerKey;
}
public String getConsumerSecret() {
return consumerSecret;
}
public void setConsumerSecret(String consumerSecret) {
this.consumerSecret = consumerSecret;
}
public String getHttpProxyHost() {
return httpProxyHost;
}
public void setHttpProxyHost(String httpProxyHost) {
this.httpProxyHost = httpProxyHost;
}
public String getHttpProxyUser() {
return httpProxyUser;
}
public void setHttpProxyUser(String httpProxyUser) {
this.httpProxyUser = httpProxyUser;
}
public String getHttpProxyPassword() {
return httpProxyPassword;
}
public void setHttpProxyPassword(String httpProxyPassword) {
this.httpProxyPassword = httpProxyPassword;
}
public int getHttpProxyPort() {
return httpProxyPort;
}
public void setHttpProxyPort(int httpProxyPort) {
this.httpProxyPort = httpProxyPort;
}
} | jmandawg/camel | components/camel-twitter/src/main/java/org/apache/camel/component/twitter/springboot/TwitterComponentConfiguration.java | Java | apache-2.0 | 3,496 |
/*
* 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 com.facebook.presto.hive;
import com.facebook.presto.hive.HdfsEnvironment.HdfsContext;
import com.facebook.presto.hive.metastore.Database;
import com.facebook.presto.hive.metastore.Partition;
import com.facebook.presto.hive.metastore.SemiTransactionalHiveMetastore;
import com.facebook.presto.hive.metastore.Storage;
import com.facebook.presto.hive.metastore.Table;
import com.facebook.presto.hive.s3.PrestoS3FileSystem;
import com.facebook.presto.spi.PrestoException;
import com.facebook.presto.spi.SchemaNotFoundException;
import com.facebook.presto.spi.SchemaTableName;
import com.facebook.presto.spi.StandardErrorCode;
import com.facebook.presto.spi.block.Block;
import com.facebook.presto.spi.type.BigintType;
import com.facebook.presto.spi.type.BooleanType;
import com.facebook.presto.spi.type.CharType;
import com.facebook.presto.spi.type.DateType;
import com.facebook.presto.spi.type.DecimalType;
import com.facebook.presto.spi.type.Decimals;
import com.facebook.presto.spi.type.DoubleType;
import com.facebook.presto.spi.type.IntegerType;
import com.facebook.presto.spi.type.RealType;
import com.facebook.presto.spi.type.SmallintType;
import com.facebook.presto.spi.type.TimestampType;
import com.facebook.presto.spi.type.TinyintType;
import com.facebook.presto.spi.type.Type;
import com.facebook.presto.spi.type.VarbinaryType;
import com.facebook.presto.spi.type.VarcharType;
import com.google.common.base.Throwables;
import com.google.common.collect.ImmutableList;
import com.google.common.primitives.Shorts;
import com.google.common.primitives.SignedBytes;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.FilterFileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.fs.permission.FsPermission;
import org.apache.hadoop.hive.common.type.HiveDecimal;
import org.apache.hadoop.hive.common.type.HiveVarchar;
import org.apache.hadoop.hive.conf.HiveConf;
import org.apache.hadoop.hive.metastore.ProtectMode;
import org.apache.hadoop.hive.ql.exec.FileSinkOperator.RecordWriter;
import org.apache.hadoop.hive.ql.io.HiveOutputFormat;
import org.apache.hadoop.hive.serde2.SerDeException;
import org.apache.hadoop.hive.serde2.Serializer;
import org.apache.hadoop.hive.serde2.io.DateWritable;
import org.apache.hadoop.hive.serde2.io.DoubleWritable;
import org.apache.hadoop.hive.serde2.io.HiveDecimalWritable;
import org.apache.hadoop.hive.serde2.io.ShortWritable;
import org.apache.hadoop.hive.serde2.io.TimestampWritable;
import org.apache.hadoop.hive.serde2.objectinspector.ObjectInspector;
import org.apache.hadoop.hive.serde2.objectinspector.ObjectInspectorFactory;
import org.apache.hadoop.hive.serde2.objectinspector.PrimitiveObjectInspector.PrimitiveCategory;
import org.apache.hadoop.hive.serde2.objectinspector.SettableStructObjectInspector;
import org.apache.hadoop.hive.serde2.objectinspector.StructField;
import org.apache.hadoop.hive.serde2.typeinfo.DecimalTypeInfo;
import org.apache.hadoop.hive.serde2.typeinfo.ListTypeInfo;
import org.apache.hadoop.hive.serde2.typeinfo.MapTypeInfo;
import org.apache.hadoop.hive.serde2.typeinfo.PrimitiveTypeInfo;
import org.apache.hadoop.hive.serde2.typeinfo.StructTypeInfo;
import org.apache.hadoop.hive.serde2.typeinfo.TypeInfo;
import org.apache.hadoop.io.BooleanWritable;
import org.apache.hadoop.io.ByteWritable;
import org.apache.hadoop.io.BytesWritable;
import org.apache.hadoop.io.FloatWritable;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapred.JobConf;
import org.apache.hadoop.mapred.Reporter;
import org.joda.time.DateTimeZone;
import java.io.IOException;
import java.math.BigInteger;
import java.sql.Date;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Properties;
import java.util.concurrent.TimeUnit;
import static com.facebook.presto.hive.HiveErrorCode.HIVE_DATABASE_LOCATION_ERROR;
import static com.facebook.presto.hive.HiveErrorCode.HIVE_FILESYSTEM_ERROR;
import static com.facebook.presto.hive.HiveErrorCode.HIVE_WRITER_DATA_ERROR;
import static com.facebook.presto.hive.HiveUtil.checkCondition;
import static com.facebook.presto.hive.HiveUtil.isArrayType;
import static com.facebook.presto.hive.HiveUtil.isMapType;
import static com.facebook.presto.hive.HiveUtil.isRowType;
import static com.facebook.presto.hive.metastore.MetastoreUtil.getProtectMode;
import static com.facebook.presto.hive.metastore.MetastoreUtil.verifyOnline;
import static com.facebook.presto.spi.StandardErrorCode.NOT_SUPPORTED;
import static com.facebook.presto.spi.type.Chars.isCharType;
import static com.google.common.base.Strings.padEnd;
import static java.lang.Float.intBitsToFloat;
import static java.lang.Math.toIntExact;
import static java.lang.String.format;
import static java.util.Objects.requireNonNull;
import static java.util.UUID.randomUUID;
import static java.util.stream.Collectors.toList;
import static org.apache.hadoop.hive.conf.HiveConf.ConfVars.COMPRESSRESULT;
import static org.apache.hadoop.hive.metastore.TableType.MANAGED_TABLE;
import static org.apache.hadoop.hive.serde2.objectinspector.primitive.PrimitiveObjectInspectorFactory.getPrimitiveJavaObjectInspector;
import static org.apache.hadoop.hive.serde2.objectinspector.primitive.PrimitiveObjectInspectorFactory.getPrimitiveWritableObjectInspector;
import static org.apache.hadoop.hive.serde2.objectinspector.primitive.PrimitiveObjectInspectorFactory.javaBooleanObjectInspector;
import static org.apache.hadoop.hive.serde2.objectinspector.primitive.PrimitiveObjectInspectorFactory.javaByteArrayObjectInspector;
import static org.apache.hadoop.hive.serde2.objectinspector.primitive.PrimitiveObjectInspectorFactory.javaByteObjectInspector;
import static org.apache.hadoop.hive.serde2.objectinspector.primitive.PrimitiveObjectInspectorFactory.javaDateObjectInspector;
import static org.apache.hadoop.hive.serde2.objectinspector.primitive.PrimitiveObjectInspectorFactory.javaDoubleObjectInspector;
import static org.apache.hadoop.hive.serde2.objectinspector.primitive.PrimitiveObjectInspectorFactory.javaFloatObjectInspector;
import static org.apache.hadoop.hive.serde2.objectinspector.primitive.PrimitiveObjectInspectorFactory.javaIntObjectInspector;
import static org.apache.hadoop.hive.serde2.objectinspector.primitive.PrimitiveObjectInspectorFactory.javaLongObjectInspector;
import static org.apache.hadoop.hive.serde2.objectinspector.primitive.PrimitiveObjectInspectorFactory.javaShortObjectInspector;
import static org.apache.hadoop.hive.serde2.objectinspector.primitive.PrimitiveObjectInspectorFactory.javaTimestampObjectInspector;
import static org.apache.hadoop.hive.serde2.objectinspector.primitive.PrimitiveObjectInspectorFactory.writableBinaryObjectInspector;
import static org.apache.hadoop.hive.serde2.objectinspector.primitive.PrimitiveObjectInspectorFactory.writableBooleanObjectInspector;
import static org.apache.hadoop.hive.serde2.objectinspector.primitive.PrimitiveObjectInspectorFactory.writableByteObjectInspector;
import static org.apache.hadoop.hive.serde2.objectinspector.primitive.PrimitiveObjectInspectorFactory.writableDateObjectInspector;
import static org.apache.hadoop.hive.serde2.objectinspector.primitive.PrimitiveObjectInspectorFactory.writableDoubleObjectInspector;
import static org.apache.hadoop.hive.serde2.objectinspector.primitive.PrimitiveObjectInspectorFactory.writableFloatObjectInspector;
import static org.apache.hadoop.hive.serde2.objectinspector.primitive.PrimitiveObjectInspectorFactory.writableHiveCharObjectInspector;
import static org.apache.hadoop.hive.serde2.objectinspector.primitive.PrimitiveObjectInspectorFactory.writableIntObjectInspector;
import static org.apache.hadoop.hive.serde2.objectinspector.primitive.PrimitiveObjectInspectorFactory.writableLongObjectInspector;
import static org.apache.hadoop.hive.serde2.objectinspector.primitive.PrimitiveObjectInspectorFactory.writableShortObjectInspector;
import static org.apache.hadoop.hive.serde2.objectinspector.primitive.PrimitiveObjectInspectorFactory.writableStringObjectInspector;
import static org.apache.hadoop.hive.serde2.objectinspector.primitive.PrimitiveObjectInspectorFactory.writableTimestampObjectInspector;
import static org.apache.hadoop.hive.serde2.typeinfo.TypeInfoFactory.getCharTypeInfo;
import static org.apache.hadoop.hive.serde2.typeinfo.TypeInfoFactory.getVarcharTypeInfo;
import static org.joda.time.DateTimeZone.UTC;
public final class HiveWriteUtils
{
@SuppressWarnings("OctalInteger")
private static final FsPermission ALL_PERMISSIONS = new FsPermission((short) 0777);
private HiveWriteUtils()
{
}
public static RecordWriter createRecordWriter(Path target, JobConf conf, Properties properties, String outputFormatName)
{
try {
boolean compress = HiveConf.getBoolVar(conf, COMPRESSRESULT);
Object writer = Class.forName(outputFormatName).getConstructor().newInstance();
return ((HiveOutputFormat<?, ?>) writer).getHiveRecordWriter(conf, target, Text.class, compress, properties, Reporter.NULL);
}
catch (IOException | ReflectiveOperationException e) {
throw new PrestoException(HIVE_WRITER_DATA_ERROR, e);
}
}
@SuppressWarnings("deprecation")
public static Serializer initializeSerializer(Configuration conf, Properties properties, String serializerName)
{
try {
Serializer result = (Serializer) Class.forName(serializerName).getConstructor().newInstance();
result.initialize(conf, properties);
return result;
}
catch (SerDeException | ReflectiveOperationException e) {
throw Throwables.propagate(e);
}
}
public static ObjectInspector getJavaObjectInspector(Type type)
{
if (type.equals(BooleanType.BOOLEAN)) {
return javaBooleanObjectInspector;
}
else if (type.equals(BigintType.BIGINT)) {
return javaLongObjectInspector;
}
else if (type.equals(IntegerType.INTEGER)) {
return javaIntObjectInspector;
}
else if (type.equals(SmallintType.SMALLINT)) {
return javaShortObjectInspector;
}
else if (type.equals(TinyintType.TINYINT)) {
return javaByteObjectInspector;
}
else if (type.equals(RealType.REAL)) {
return javaFloatObjectInspector;
}
else if (type.equals(DoubleType.DOUBLE)) {
return javaDoubleObjectInspector;
}
else if (type instanceof VarcharType) {
return writableStringObjectInspector;
}
else if (type instanceof CharType) {
return writableHiveCharObjectInspector;
}
else if (type.equals(VarbinaryType.VARBINARY)) {
return javaByteArrayObjectInspector;
}
else if (type.equals(DateType.DATE)) {
return javaDateObjectInspector;
}
else if (type.equals(TimestampType.TIMESTAMP)) {
return javaTimestampObjectInspector;
}
else if (type instanceof DecimalType) {
DecimalType decimalType = (DecimalType) type;
return getPrimitiveJavaObjectInspector(new DecimalTypeInfo(decimalType.getPrecision(), decimalType.getScale()));
}
else if (isArrayType(type)) {
return ObjectInspectorFactory.getStandardListObjectInspector(getJavaObjectInspector(type.getTypeParameters().get(0)));
}
else if (isMapType(type)) {
ObjectInspector keyObjectInspector = getJavaObjectInspector(type.getTypeParameters().get(0));
ObjectInspector valueObjectInspector = getJavaObjectInspector(type.getTypeParameters().get(1));
return ObjectInspectorFactory.getStandardMapObjectInspector(keyObjectInspector, valueObjectInspector);
}
else if (isRowType(type)) {
return ObjectInspectorFactory.getStandardStructObjectInspector(
type.getTypeSignature().getParameters().stream()
.map(parameter -> parameter.getNamedTypeSignature().getName())
.collect(toList()),
type.getTypeParameters().stream()
.map(HiveWriteUtils::getJavaObjectInspector)
.collect(toList()));
}
throw new IllegalArgumentException("unsupported type: " + type);
}
public static Object getField(Type type, Block block, int position)
{
if (block.isNull(position)) {
return null;
}
if (BooleanType.BOOLEAN.equals(type)) {
return type.getBoolean(block, position);
}
if (BigintType.BIGINT.equals(type)) {
return type.getLong(block, position);
}
if (IntegerType.INTEGER.equals(type)) {
return (int) type.getLong(block, position);
}
if (SmallintType.SMALLINT.equals(type)) {
return (short) type.getLong(block, position);
}
if (TinyintType.TINYINT.equals(type)) {
return (byte) type.getLong(block, position);
}
if (RealType.REAL.equals(type)) {
return intBitsToFloat((int) type.getLong(block, position));
}
if (DoubleType.DOUBLE.equals(type)) {
return type.getDouble(block, position);
}
if (type instanceof VarcharType) {
return new Text(type.getSlice(block, position).getBytes());
}
if (type instanceof CharType) {
CharType charType = (CharType) type;
return new Text(padEnd(type.getSlice(block, position).toStringUtf8(), charType.getLength(), ' '));
}
if (VarbinaryType.VARBINARY.equals(type)) {
return type.getSlice(block, position).getBytes();
}
if (DateType.DATE.equals(type)) {
long days = type.getLong(block, position);
return new Date(UTC.getMillisKeepLocal(DateTimeZone.getDefault(), TimeUnit.DAYS.toMillis(days)));
}
if (TimestampType.TIMESTAMP.equals(type)) {
long millisUtc = type.getLong(block, position);
return new Timestamp(millisUtc);
}
if (type instanceof DecimalType) {
DecimalType decimalType = (DecimalType) type;
return getHiveDecimal(decimalType, block, position);
}
if (isArrayType(type)) {
Type elementType = type.getTypeParameters().get(0);
Block arrayBlock = block.getObject(position, Block.class);
List<Object> list = new ArrayList<>(arrayBlock.getPositionCount());
for (int i = 0; i < arrayBlock.getPositionCount(); i++) {
Object element = getField(elementType, arrayBlock, i);
list.add(element);
}
return Collections.unmodifiableList(list);
}
if (isMapType(type)) {
Type keyType = type.getTypeParameters().get(0);
Type valueType = type.getTypeParameters().get(1);
Block mapBlock = block.getObject(position, Block.class);
Map<Object, Object> map = new HashMap<>();
for (int i = 0; i < mapBlock.getPositionCount(); i += 2) {
Object key = getField(keyType, mapBlock, i);
Object value = getField(valueType, mapBlock, i + 1);
map.put(key, value);
}
return Collections.unmodifiableMap(map);
}
if (isRowType(type)) {
Block rowBlock = block.getObject(position, Block.class);
List<Type> fieldTypes = type.getTypeParameters();
checkCondition(fieldTypes.size() == rowBlock.getPositionCount(), StandardErrorCode.GENERIC_INTERNAL_ERROR, "Expected row value field count does not match type field count");
List<Object> row = new ArrayList<>(rowBlock.getPositionCount());
for (int i = 0; i < rowBlock.getPositionCount(); i++) {
Object element = getField(fieldTypes.get(i), rowBlock, i);
row.add(element);
}
return Collections.unmodifiableList(row);
}
throw new PrestoException(NOT_SUPPORTED, "unsupported type: " + type);
}
public static void checkTableIsWritable(Table table, boolean writesToNonManagedTablesEnabled)
{
if (!writesToNonManagedTablesEnabled && !table.getTableType().equals(MANAGED_TABLE.toString())) {
throw new PrestoException(NOT_SUPPORTED, "Cannot write to non-managed Hive table");
}
checkWritable(
new SchemaTableName(table.getDatabaseName(), table.getTableName()),
Optional.empty(),
getProtectMode(table),
table.getParameters(),
table.getStorage());
}
public static void checkPartitionIsWritable(String partitionName, Partition partition)
{
checkWritable(
new SchemaTableName(partition.getDatabaseName(), partition.getTableName()),
Optional.of(partitionName),
getProtectMode(partition),
partition.getParameters(),
partition.getStorage());
}
private static void checkWritable(
SchemaTableName tableName,
Optional<String> partitionName,
ProtectMode protectMode,
Map<String, String> parameters,
Storage storage)
{
String tablePartitionDescription = "Table '" + tableName + "'";
if (partitionName.isPresent()) {
tablePartitionDescription += " partition '" + partitionName.get() + "'";
}
// verify online
verifyOnline(tableName, partitionName, protectMode, parameters);
// verify not read only
if (protectMode.readOnly) {
throw new HiveReadOnlyException(tableName, partitionName);
}
// verify sorting
if (storage.isSorted()) {
throw new PrestoException(NOT_SUPPORTED, format("Inserting into bucketed sorted tables is not supported. %s", tablePartitionDescription));
}
// verify skew info
if (storage.isSkewed()) {
throw new PrestoException(NOT_SUPPORTED, format("Inserting into bucketed tables with skew is not supported. %s", tablePartitionDescription));
}
}
public static Path getTableDefaultLocation(HdfsContext context, SemiTransactionalHiveMetastore metastore, HdfsEnvironment hdfsEnvironment, String schemaName, String tableName)
{
Optional<String> location = getDatabase(metastore, schemaName).getLocation();
if (!location.isPresent() || location.get().isEmpty()) {
throw new PrestoException(HIVE_DATABASE_LOCATION_ERROR, format("Database '%s' location is not set", schemaName));
}
Path databasePath = new Path(location.get());
if (!isS3FileSystem(context, hdfsEnvironment, databasePath)) {
if (!pathExists(context, hdfsEnvironment, databasePath)) {
throw new PrestoException(HIVE_DATABASE_LOCATION_ERROR, format("Database '%s' location does not exist: %s", schemaName, databasePath));
}
if (!isDirectory(context, hdfsEnvironment, databasePath)) {
throw new PrestoException(HIVE_DATABASE_LOCATION_ERROR, format("Database '%s' location is not a directory: %s", schemaName, databasePath));
}
}
return new Path(databasePath, tableName);
}
private static Database getDatabase(SemiTransactionalHiveMetastore metastore, String database)
{
return metastore.getDatabase(database).orElseThrow(() -> new SchemaNotFoundException(database));
}
public static boolean pathExists(HdfsContext context, HdfsEnvironment hdfsEnvironment, Path path)
{
try {
return hdfsEnvironment.getFileSystem(context, path).exists(path);
}
catch (IOException e) {
throw new PrestoException(HIVE_FILESYSTEM_ERROR, "Failed checking path: " + path, e);
}
}
public static boolean isS3FileSystem(HdfsContext context, HdfsEnvironment hdfsEnvironment, Path path)
{
try {
return getRawFileSystem(hdfsEnvironment.getFileSystem(context, path)) instanceof PrestoS3FileSystem;
}
catch (IOException e) {
throw new PrestoException(HIVE_FILESYSTEM_ERROR, "Failed checking path: " + path, e);
}
}
public static boolean isViewFileSystem(HdfsContext context, HdfsEnvironment hdfsEnvironment, Path path)
{
try {
// Hadoop 1.x does not have the ViewFileSystem class
return getRawFileSystem(hdfsEnvironment.getFileSystem(context, path))
.getClass().getName().equals("org.apache.hadoop.fs.viewfs.ViewFileSystem");
}
catch (IOException e) {
throw new PrestoException(HIVE_FILESYSTEM_ERROR, "Failed checking path: " + path, e);
}
}
private static FileSystem getRawFileSystem(FileSystem fileSystem)
{
if (fileSystem instanceof FilterFileSystem) {
return getRawFileSystem(((FilterFileSystem) fileSystem).getRawFileSystem());
}
return fileSystem;
}
private static boolean isDirectory(HdfsContext context, HdfsEnvironment hdfsEnvironment, Path path)
{
try {
return hdfsEnvironment.getFileSystem(context, path).isDirectory(path);
}
catch (IOException e) {
throw new PrestoException(HIVE_FILESYSTEM_ERROR, "Failed checking path: " + path, e);
}
}
public static Path createTemporaryPath(HdfsContext context, HdfsEnvironment hdfsEnvironment, Path targetPath)
{
// use a per-user temporary directory to avoid permission problems
String temporaryPrefix = "/tmp/presto-" + context.getIdentity().getUser();
// use relative temporary directory on ViewFS
if (isViewFileSystem(context, hdfsEnvironment, targetPath)) {
temporaryPrefix = ".hive-staging";
}
// create a temporary directory on the same filesystem
Path temporaryRoot = new Path(targetPath, temporaryPrefix);
Path temporaryPath = new Path(temporaryRoot, randomUUID().toString());
createDirectory(context, hdfsEnvironment, temporaryPath);
return temporaryPath;
}
public static void createDirectory(HdfsContext context, HdfsEnvironment hdfsEnvironment, Path path)
{
try {
if (!hdfsEnvironment.getFileSystem(context, path).mkdirs(path, ALL_PERMISSIONS)) {
throw new IOException("mkdirs returned false");
}
}
catch (IOException e) {
throw new PrestoException(HIVE_FILESYSTEM_ERROR, "Failed to create directory: " + path, e);
}
// explicitly set permission since the default umask overrides it on creation
try {
hdfsEnvironment.getFileSystem(context, path).setPermission(path, ALL_PERMISSIONS);
}
catch (IOException e) {
throw new PrestoException(HIVE_FILESYSTEM_ERROR, "Failed to set permission on directory: " + path, e);
}
}
public static boolean isWritableType(HiveType hiveType)
{
return isWritableType(hiveType.getTypeInfo());
}
private static boolean isWritableType(TypeInfo typeInfo)
{
switch (typeInfo.getCategory()) {
case PRIMITIVE:
PrimitiveCategory primitiveCategory = ((PrimitiveTypeInfo) typeInfo).getPrimitiveCategory();
return isWritablePrimitiveType(primitiveCategory);
case MAP:
MapTypeInfo mapTypeInfo = (MapTypeInfo) typeInfo;
return isWritableType(mapTypeInfo.getMapKeyTypeInfo()) && isWritableType(mapTypeInfo.getMapValueTypeInfo());
case LIST:
ListTypeInfo listTypeInfo = (ListTypeInfo) typeInfo;
return isWritableType(listTypeInfo.getListElementTypeInfo());
case STRUCT:
StructTypeInfo structTypeInfo = (StructTypeInfo) typeInfo;
return structTypeInfo.getAllStructFieldTypeInfos().stream().allMatch(HiveWriteUtils::isWritableType);
}
return false;
}
private static boolean isWritablePrimitiveType(PrimitiveCategory primitiveCategory)
{
switch (primitiveCategory) {
case BOOLEAN:
case LONG:
case INT:
case SHORT:
case BYTE:
case FLOAT:
case DOUBLE:
case STRING:
case DATE:
case TIMESTAMP:
case BINARY:
case DECIMAL:
case VARCHAR:
case CHAR:
return true;
}
return false;
}
public static List<ObjectInspector> getRowColumnInspectors(List<Type> types)
{
return types.stream()
.map(HiveWriteUtils::getRowColumnInspector)
.collect(toList());
}
public static ObjectInspector getRowColumnInspector(Type type)
{
if (type.equals(BooleanType.BOOLEAN)) {
return writableBooleanObjectInspector;
}
if (type.equals(BigintType.BIGINT)) {
return writableLongObjectInspector;
}
if (type.equals(IntegerType.INTEGER)) {
return writableIntObjectInspector;
}
if (type.equals(SmallintType.SMALLINT)) {
return writableShortObjectInspector;
}
if (type.equals(TinyintType.TINYINT)) {
return writableByteObjectInspector;
}
if (type.equals(RealType.REAL)) {
return writableFloatObjectInspector;
}
if (type.equals(DoubleType.DOUBLE)) {
return writableDoubleObjectInspector;
}
if (type instanceof VarcharType) {
VarcharType varcharType = (VarcharType) type;
int varcharLength = varcharType.getLength();
// VARCHAR columns with the length less than or equal to 65535 are supported natively by Hive
if (varcharLength <= HiveVarchar.MAX_VARCHAR_LENGTH) {
return getPrimitiveWritableObjectInspector(getVarcharTypeInfo(varcharLength));
}
// Unbounded VARCHAR is not supported by Hive.
// Values for such columns must be stored as STRING in Hive
else if (varcharLength == VarcharType.UNBOUNDED_LENGTH) {
return writableStringObjectInspector;
}
}
if (isCharType(type)) {
CharType charType = (CharType) type;
int charLength = charType.getLength();
return getPrimitiveWritableObjectInspector(getCharTypeInfo(charLength));
}
if (type.equals(VarbinaryType.VARBINARY)) {
return writableBinaryObjectInspector;
}
if (type.equals(DateType.DATE)) {
return writableDateObjectInspector;
}
if (type.equals(TimestampType.TIMESTAMP)) {
return writableTimestampObjectInspector;
}
if (type instanceof DecimalType) {
DecimalType decimalType = (DecimalType) type;
return getPrimitiveWritableObjectInspector(new DecimalTypeInfo(decimalType.getPrecision(), decimalType.getScale()));
}
if (isArrayType(type) || isMapType(type) || isRowType(type)) {
return getJavaObjectInspector(type);
}
throw new IllegalArgumentException("unsupported type: " + type);
}
public static FieldSetter createFieldSetter(SettableStructObjectInspector rowInspector, Object row, StructField field, Type type)
{
if (type.equals(BooleanType.BOOLEAN)) {
return new BooleanFieldSetter(rowInspector, row, field);
}
if (type.equals(BigintType.BIGINT)) {
return new BigintFieldBuilder(rowInspector, row, field);
}
if (type.equals(IntegerType.INTEGER)) {
return new IntFieldSetter(rowInspector, row, field);
}
if (type.equals(SmallintType.SMALLINT)) {
return new SmallintFieldSetter(rowInspector, row, field);
}
if (type.equals(TinyintType.TINYINT)) {
return new TinyintFieldSetter(rowInspector, row, field);
}
if (type.equals(RealType.REAL)) {
return new FloatFieldSetter(rowInspector, row, field);
}
if (type.equals(DoubleType.DOUBLE)) {
return new DoubleFieldSetter(rowInspector, row, field);
}
if (type instanceof VarcharType) {
return new VarcharFieldSetter(rowInspector, row, field, type);
}
if (type instanceof CharType) {
return new CharFieldSetter(rowInspector, row, field, type);
}
if (type.equals(VarbinaryType.VARBINARY)) {
return new BinaryFieldSetter(rowInspector, row, field);
}
if (type.equals(DateType.DATE)) {
return new DateFieldSetter(rowInspector, row, field);
}
if (type.equals(TimestampType.TIMESTAMP)) {
return new TimestampFieldSetter(rowInspector, row, field);
}
if (type instanceof DecimalType) {
DecimalType decimalType = (DecimalType) type;
return new DecimalFieldSetter(rowInspector, row, field, decimalType);
}
if (isArrayType(type)) {
return new ArrayFieldSetter(rowInspector, row, field, type.getTypeParameters().get(0));
}
if (isMapType(type)) {
return new MapFieldSetter(rowInspector, row, field, type.getTypeParameters().get(0), type.getTypeParameters().get(1));
}
if (isRowType(type)) {
return new RowFieldSetter(rowInspector, row, field, type.getTypeParameters());
}
throw new IllegalArgumentException("unsupported type: " + type);
}
public abstract static class FieldSetter
{
protected final SettableStructObjectInspector rowInspector;
protected final Object row;
protected final StructField field;
protected FieldSetter(SettableStructObjectInspector rowInspector, Object row, StructField field)
{
this.rowInspector = requireNonNull(rowInspector, "rowInspector is null");
this.row = requireNonNull(row, "row is null");
this.field = requireNonNull(field, "field is null");
}
public abstract void setField(Block block, int position);
}
private static class BooleanFieldSetter
extends FieldSetter
{
private final BooleanWritable value = new BooleanWritable();
public BooleanFieldSetter(SettableStructObjectInspector rowInspector, Object row, StructField field)
{
super(rowInspector, row, field);
}
@Override
public void setField(Block block, int position)
{
value.set(BooleanType.BOOLEAN.getBoolean(block, position));
rowInspector.setStructFieldData(row, field, value);
}
}
private static class BigintFieldBuilder
extends FieldSetter
{
private final LongWritable value = new LongWritable();
public BigintFieldBuilder(SettableStructObjectInspector rowInspector, Object row, StructField field)
{
super(rowInspector, row, field);
}
@Override
public void setField(Block block, int position)
{
value.set(BigintType.BIGINT.getLong(block, position));
rowInspector.setStructFieldData(row, field, value);
}
}
private static class IntFieldSetter
extends FieldSetter
{
private final IntWritable value = new IntWritable();
public IntFieldSetter(SettableStructObjectInspector rowInspector, Object row, StructField field)
{
super(rowInspector, row, field);
}
@Override
public void setField(Block block, int position)
{
value.set(toIntExact(IntegerType.INTEGER.getLong(block, position)));
rowInspector.setStructFieldData(row, field, value);
}
}
private static class SmallintFieldSetter
extends FieldSetter
{
private final ShortWritable value = new ShortWritable();
public SmallintFieldSetter(SettableStructObjectInspector rowInspector, Object row, StructField field)
{
super(rowInspector, row, field);
}
@Override
public void setField(Block block, int position)
{
value.set(Shorts.checkedCast(SmallintType.SMALLINT.getLong(block, position)));
rowInspector.setStructFieldData(row, field, value);
}
}
private static class TinyintFieldSetter
extends FieldSetter
{
private final ByteWritable value = new ByteWritable();
public TinyintFieldSetter(SettableStructObjectInspector rowInspector, Object row, StructField field)
{
super(rowInspector, row, field);
}
@Override
public void setField(Block block, int position)
{
value.set(SignedBytes.checkedCast(TinyintType.TINYINT.getLong(block, position)));
rowInspector.setStructFieldData(row, field, value);
}
}
private static class DoubleFieldSetter
extends FieldSetter
{
private final DoubleWritable value = new DoubleWritable();
public DoubleFieldSetter(SettableStructObjectInspector rowInspector, Object row, StructField field)
{
super(rowInspector, row, field);
}
@Override
public void setField(Block block, int position)
{
value.set(DoubleType.DOUBLE.getDouble(block, position));
rowInspector.setStructFieldData(row, field, value);
}
}
private static class FloatFieldSetter
extends FieldSetter
{
private final FloatWritable value = new FloatWritable();
public FloatFieldSetter(SettableStructObjectInspector rowInspector, Object row, StructField field)
{
super(rowInspector, row, field);
}
@Override
public void setField(Block block, int position)
{
value.set(intBitsToFloat((int) RealType.REAL.getLong(block, position)));
rowInspector.setStructFieldData(row, field, value);
}
}
private static class VarcharFieldSetter
extends FieldSetter
{
private final Text value = new Text();
private final Type type;
public VarcharFieldSetter(SettableStructObjectInspector rowInspector, Object row, StructField field, Type type)
{
super(rowInspector, row, field);
this.type = type;
}
@Override
public void setField(Block block, int position)
{
value.set(type.getSlice(block, position).getBytes());
rowInspector.setStructFieldData(row, field, value);
}
}
private static class CharFieldSetter
extends FieldSetter
{
private final Text value = new Text();
private final Type type;
public CharFieldSetter(SettableStructObjectInspector rowInspector, Object row, StructField field, Type type)
{
super(rowInspector, row, field);
this.type = type;
}
@Override
public void setField(Block block, int position)
{
value.set(type.getSlice(block, position).getBytes());
rowInspector.setStructFieldData(row, field, value);
}
}
private static class BinaryFieldSetter
extends FieldSetter
{
private final BytesWritable value = new BytesWritable();
public BinaryFieldSetter(SettableStructObjectInspector rowInspector, Object row, StructField field)
{
super(rowInspector, row, field);
}
@Override
public void setField(Block block, int position)
{
byte[] bytes = VarbinaryType.VARBINARY.getSlice(block, position).getBytes();
value.set(bytes, 0, bytes.length);
rowInspector.setStructFieldData(row, field, value);
}
}
private static class DateFieldSetter
extends FieldSetter
{
private final DateWritable value = new DateWritable();
public DateFieldSetter(SettableStructObjectInspector rowInspector, Object row, StructField field)
{
super(rowInspector, row, field);
}
@Override
public void setField(Block block, int position)
{
value.set(toIntExact(DateType.DATE.getLong(block, position)));
rowInspector.setStructFieldData(row, field, value);
}
}
private static class TimestampFieldSetter
extends FieldSetter
{
private final TimestampWritable value = new TimestampWritable();
public TimestampFieldSetter(SettableStructObjectInspector rowInspector, Object row, StructField field)
{
super(rowInspector, row, field);
}
@Override
public void setField(Block block, int position)
{
long millisUtc = TimestampType.TIMESTAMP.getLong(block, position);
value.setTime(millisUtc);
rowInspector.setStructFieldData(row, field, value);
}
}
private static class DecimalFieldSetter
extends FieldSetter
{
private final HiveDecimalWritable value = new HiveDecimalWritable();
private final DecimalType decimalType;
public DecimalFieldSetter(SettableStructObjectInspector rowInspector, Object row, StructField field, DecimalType decimalType)
{
super(rowInspector, row, field);
this.decimalType = decimalType;
}
@Override
public void setField(Block block, int position)
{
value.set(getHiveDecimal(decimalType, block, position));
rowInspector.setStructFieldData(row, field, value);
}
}
private static HiveDecimal getHiveDecimal(DecimalType decimalType, Block block, int position)
{
BigInteger unscaledValue;
if (decimalType.isShort()) {
unscaledValue = BigInteger.valueOf(decimalType.getLong(block, position));
}
else {
unscaledValue = Decimals.decodeUnscaledValue(decimalType.getSlice(block, position));
}
return HiveDecimal.create(unscaledValue, decimalType.getScale());
}
private static class ArrayFieldSetter
extends FieldSetter
{
private final Type elementType;
public ArrayFieldSetter(SettableStructObjectInspector rowInspector, Object row, StructField field, Type elementType)
{
super(rowInspector, row, field);
this.elementType = requireNonNull(elementType, "elementType is null");
}
@Override
public void setField(Block block, int position)
{
Block arrayBlock = block.getObject(position, Block.class);
List<Object> list = new ArrayList<>(arrayBlock.getPositionCount());
for (int i = 0; i < arrayBlock.getPositionCount(); i++) {
Object element = getField(elementType, arrayBlock, i);
list.add(element);
}
rowInspector.setStructFieldData(row, field, list);
}
}
private static class MapFieldSetter
extends FieldSetter
{
private final Type keyType;
private final Type valueType;
public MapFieldSetter(SettableStructObjectInspector rowInspector, Object row, StructField field, Type keyType, Type valueType)
{
super(rowInspector, row, field);
this.keyType = requireNonNull(keyType, "keyType is null");
this.valueType = requireNonNull(valueType, "valueType is null");
}
@Override
public void setField(Block block, int position)
{
Block mapBlock = block.getObject(position, Block.class);
Map<Object, Object> map = new HashMap<>(mapBlock.getPositionCount() * 2);
for (int i = 0; i < mapBlock.getPositionCount(); i += 2) {
Object key = getField(keyType, mapBlock, i);
Object value = getField(valueType, mapBlock, i + 1);
map.put(key, value);
}
rowInspector.setStructFieldData(row, field, map);
}
}
private static class RowFieldSetter
extends FieldSetter
{
private final List<Type> fieldTypes;
public RowFieldSetter(SettableStructObjectInspector rowInspector, Object row, StructField field, List<Type> fieldTypes)
{
super(rowInspector, row, field);
this.fieldTypes = ImmutableList.copyOf(fieldTypes);
}
@Override
public void setField(Block block, int position)
{
Block rowBlock = block.getObject(position, Block.class);
// TODO reuse row object and use FieldSetters, like we do at the top level
// Ideally, we'd use the same recursive structure starting from the top, but
// this requires modeling row types in the same way we model table rows
// (multiple blocks vs all fields packed in a single block)
List<Object> value = new ArrayList<>(fieldTypes.size());
for (int i = 0; i < fieldTypes.size(); i++) {
Object element = getField(fieldTypes.get(i), rowBlock, i);
value.add(element);
}
rowInspector.setStructFieldData(row, field, value);
}
}
}
| gh351135612/presto | presto-hive/src/main/java/com/facebook/presto/hive/HiveWriteUtils.java | Java | apache-2.0 | 42,741 |
// Copyright 2000-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
package com.siyeh.ig.logging;
import com.intellij.codeInspection.CommonQuickFixBundle;
import com.intellij.codeInspection.ProblemDescriptor;
import com.intellij.codeInspection.ui.ListTable;
import com.intellij.codeInspection.ui.ListWrappingTableModel;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.util.InvalidDataException;
import com.intellij.openapi.util.WriteExternalException;
import com.intellij.psi.*;
import com.intellij.psi.util.PsiUtil;
import com.intellij.util.xmlb.Accessor;
import com.intellij.util.xmlb.SerializationFilterBase;
import com.intellij.util.xmlb.XmlSerializer;
import com.siyeh.InspectionGadgetsBundle;
import com.siyeh.ig.BaseInspection;
import com.siyeh.ig.BaseInspectionVisitor;
import com.siyeh.ig.InspectionGadgetsFix;
import com.siyeh.ig.PsiReplacementUtil;
import com.siyeh.ig.psiutils.ClassUtils;
import com.siyeh.ig.psiutils.CommentTracker;
import com.siyeh.ig.ui.UiUtils;
import org.jdom.Element;
import org.jetbrains.annotations.NonNls;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import javax.swing.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class LoggerInitializedWithForeignClassInspection extends BaseInspection {
@NonNls private static final String DEFAULT_FACTORY_CLASS_NAMES =
// Log4J 1
"org.apache.log4j.Logger," +
// SLF4J
"org.slf4j.LoggerFactory," +
// Apache Commons Logging
"org.apache.commons.logging.LogFactory," +
// Java Util Logging
"java.util.logging.Logger," +
// Log4J 2
"org.apache.logging.log4j.LogManager";
@NonNls private static final String DEFAULT_FACTORY_METHOD_NAMES =
//Log4J 1
"getLogger," +
// SLF4J
"getLogger," +
// Apache Commons Logging
"getLog," +
// Java Util Logging
"getLogger," +
// Log4J 2
"getLogger";
protected final List<String> loggerFactoryClassNames = new ArrayList<>();
protected final List<String> loggerFactoryMethodNames = new ArrayList<>();
@SuppressWarnings("PublicField")
public String loggerClassName = DEFAULT_FACTORY_CLASS_NAMES;
@SuppressWarnings("PublicField")
public @NonNls String loggerFactoryMethodName = DEFAULT_FACTORY_METHOD_NAMES;
{
parseString(loggerClassName, loggerFactoryClassNames);
parseString(loggerFactoryMethodName, loggerFactoryMethodNames);
}
@Override
public JComponent createOptionsPanel() {
final ListTable table = new ListTable(
new ListWrappingTableModel(Arrays.asList(loggerFactoryClassNames, loggerFactoryMethodNames),
InspectionGadgetsBundle.message("logger.factory.class.name"),
InspectionGadgetsBundle.message("logger.factory.method.name")));
final String title = InspectionGadgetsBundle.message("logger.initialized.with.foreign.options.title");
return UiUtils.createAddRemoveTreeClassChooserPanel(table, title);
}
@Override
@NotNull
protected String buildErrorString(Object... infos) {
return InspectionGadgetsBundle.message("logger.initialized.with.foreign.class.problem.descriptor");
}
@Override
@Nullable
protected InspectionGadgetsFix buildFix(Object... infos) {
return new LoggerInitializedWithForeignClassFix((String)infos[0]);
}
@Override
public BaseInspectionVisitor buildVisitor() {
return new LoggerInitializedWithForeignClassVisitor();
}
@Override
public void readSettings(@NotNull Element element) throws InvalidDataException {
super.readSettings(element);
parseString(loggerClassName, loggerFactoryClassNames);
parseString(loggerFactoryMethodName, loggerFactoryMethodNames);
if (loggerFactoryClassNames.size() != loggerFactoryMethodNames.size() || loggerFactoryClassNames.isEmpty()) {
parseString(DEFAULT_FACTORY_CLASS_NAMES, loggerFactoryClassNames);
parseString(DEFAULT_FACTORY_METHOD_NAMES, loggerFactoryMethodNames);
}
}
@Override
public void writeSettings(@NotNull Element element) throws WriteExternalException {
loggerClassName = formatString(loggerFactoryClassNames);
loggerFactoryMethodName = formatString(loggerFactoryMethodNames);
if (loggerFactoryMethodName.equals(DEFAULT_FACTORY_METHOD_NAMES) && loggerClassName.equals(DEFAULT_FACTORY_CLASS_NAMES)) {
// to prevent changing inspection profile with new default, which is mistakenly always written because of bug in serialization below.
loggerFactoryMethodName = "getLogger," +
"getLogger," +
"getLog," +
"getLogger";
// these broken settings are restored correctly in readSettings()
}
XmlSerializer.serializeInto(this, element, new SerializationFilterBase() {
@Override
protected boolean accepts(@NotNull Accessor accessor, @NotNull Object bean, @Nullable Object beanValue) {
final @NonNls String factoryName = accessor.getName();
if ("loggerClassName".equals(factoryName) && DEFAULT_FACTORY_CLASS_NAMES.equals(beanValue)) {
return false;
}
if ("loggerFactoryMethodNames".equals(factoryName) && DEFAULT_FACTORY_METHOD_NAMES.equals(beanValue)) {
return false;
}
return true;
}
});
}
private static final class LoggerInitializedWithForeignClassFix extends InspectionGadgetsFix {
private final String newClassName;
private LoggerInitializedWithForeignClassFix(String newClassName) {
this.newClassName = newClassName;
}
@Override
@NotNull
public String getName() {
return CommonQuickFixBundle.message("fix.replace.with.x", newClassName+".class");
}
@NotNull
@Override
public String getFamilyName() {
return InspectionGadgetsBundle.message("logger.initialized.with.foreign.class.fix.family.name");
}
@Override
protected void doFix(Project project, ProblemDescriptor descriptor) {
final PsiElement element = descriptor.getPsiElement();
if (!(element instanceof PsiClassObjectAccessExpression)) {
return;
}
final PsiClassObjectAccessExpression classObjectAccessExpression = (PsiClassObjectAccessExpression)element;
PsiReplacementUtil.replaceExpression(classObjectAccessExpression, newClassName + ".class", new CommentTracker());
}
}
private class LoggerInitializedWithForeignClassVisitor extends BaseInspectionVisitor {
@Override
public void visitClassObjectAccessExpression(PsiClassObjectAccessExpression expression) {
super.visitClassObjectAccessExpression(expression);
PsiElement parent = expression.getParent();
if (parent instanceof PsiReferenceExpression) {
final PsiReferenceExpression referenceExpression = (PsiReferenceExpression)parent;
if (!expression.equals(referenceExpression.getQualifierExpression())) {
return;
}
@NonNls final String name = referenceExpression.getReferenceName();
if (!"getName".equals(name)) {
return;
}
final PsiElement grandParent = referenceExpression.getParent();
if (!(grandParent instanceof PsiMethodCallExpression)) {
return;
}
final PsiMethodCallExpression methodCallExpression = (PsiMethodCallExpression)grandParent;
final PsiExpressionList list = methodCallExpression.getArgumentList();
if (!list.isEmpty()) {
return;
}
parent = methodCallExpression.getParent();
}
if (!(parent instanceof PsiExpressionList)) {
return;
}
final PsiElement grandParent = parent.getParent();
if (!(grandParent instanceof PsiMethodCallExpression)) {
return;
}
final PsiMethodCallExpression methodCallExpression = (PsiMethodCallExpression)grandParent;
final PsiExpressionList argumentList = methodCallExpression.getArgumentList();
final PsiExpression[] expressions = argumentList.getExpressions();
if (expressions.length != 1) {
return;
}
PsiClass containingClass = ClassUtils.getContainingClass(expression);
while (containingClass instanceof PsiAnonymousClass) {
containingClass = ClassUtils.getContainingClass(containingClass);
}
if (containingClass == null) {
return;
}
final String containingClassName = containingClass.getName();
if (containingClassName == null) {
return;
}
final PsiMethod method = methodCallExpression.resolveMethod();
if (method == null) {
return;
}
final PsiClass aClass = method.getContainingClass();
if (aClass == null) {
return;
}
final String className = aClass.getQualifiedName();
final int index = loggerFactoryClassNames.indexOf(className);
if (index < 0) {
return;
}
final PsiReferenceExpression methodExpression = methodCallExpression.getMethodExpression();
final String referenceName = methodExpression.getReferenceName();
final String loggerFactoryMethodName = loggerFactoryMethodNames.get(index);
if (!loggerFactoryMethodName.equals(referenceName)) {
return;
}
final PsiTypeElement operand = expression.getOperand();
final PsiClass initializerClass = PsiUtil.resolveClassInClassTypeOnly(operand.getType());
if (initializerClass == null) {
return;
}
if (containingClass.equals(initializerClass)) {
return;
}
registerError(expression, containingClassName);
}
}
}
| dahlstrom-g/intellij-community | plugins/InspectionGadgets/src/com/siyeh/ig/logging/LoggerInitializedWithForeignClassInspection.java | Java | apache-2.0 | 9,735 |
package com.cedarsoftware.util;
import org.junit.Assert;
import org.junit.Test;
import java.lang.reflect.Constructor;
import java.lang.reflect.Modifier;
import java.net.InetAddress;
/**
* useful InetAddress Utilities
*
* @author Kenneth Partlow
* <br>
* Copyright (c) Cedar Software LLC
* <br><br>
* 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
* <br><br>
* http://www.apache.org/licenses/LICENSE-2.0
* <br><br>
* 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.
*/
public class TestInetAddressUtilities
{
@Test
public void testMapUtilitiesConstructor() throws Exception
{
Constructor<InetAddressUtilities> con = InetAddressUtilities.class.getDeclaredConstructor();
Assert.assertEquals(Modifier.PRIVATE, con.getModifiers() & Modifier.PRIVATE);
con.setAccessible(true);
Assert.assertNotNull(con.newInstance());
}
@Test
public void testGetIpAddress() throws Exception {
byte[] bytes = InetAddress.getLocalHost().getAddress();
Assert.assertArrayEquals(bytes, InetAddressUtilities.getIpAddress());
}
@Test
public void testGetLocalHost() throws Exception {
String name = InetAddress.getLocalHost().getHostName();
Assert.assertEquals(name, InetAddressUtilities.getHostName());
}
}
| pluto-build/java-util | src/test/java/com/cedarsoftware/util/TestInetAddressUtilities.java | Java | apache-2.0 | 1,810 |
// Copyright 2000-2019 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
package com.intellij.java.psi.formatter.java;
import com.intellij.ide.highlighter.JavaFileType;
import com.intellij.openapi.util.TextRange;
import com.intellij.psi.codeStyle.CommonCodeStyleSettings;
import com.intellij.util.IncorrectOperationException;
import static com.intellij.formatting.FormatterTestUtils.Action.REFORMAT_WITH_CONTEXT;
/**
* Is intended to hold specific java formatting tests for alignment settings (
* {@code Project Settings - Code Style - Alignment and Braces}).
*
* @author Denis Zhdanov
*/
public class JavaFormatterAlignmentTest extends AbstractJavaFormatterTest {
public void testChainedMethodsAlignment() {
// Inspired by IDEA-30369
getSettings().ALIGN_MULTILINE_CHAINED_METHODS = true;
getSettings().METHOD_CALL_CHAIN_WRAP = CommonCodeStyleSettings.WRAP_AS_NEEDED;
getSettings().getRootSettings().getIndentOptions(JavaFileType.INSTANCE).CONTINUATION_INDENT_SIZE = 8;
doTest();
}
public void testMethodAndChainedField() {
// Inspired by IDEA-79806
getSettings().ALIGN_MULTILINE_CHAINED_METHODS = true;
doMethodTest(
"Holder.INSTANCE\n" +
" .foo();",
"Holder.INSTANCE\n" +
" .foo();"
);
}
public void testChainedMethodWithComments() {
getSettings().ALIGN_MULTILINE_CHAINED_METHODS = true;
doMethodTest("AAAAA.b()\n" +
".c() // comment after line\n" +
".d()\n" +
".e();",
"AAAAA.b()\n" +
" .c() // comment after line\n" +
" .d()\n" +
" .e();");
}
public void testChainedMethodWithBlockComment() {
getSettings().ALIGN_MULTILINE_CHAINED_METHODS = true;
doTextTest("class X {\n" +
" public void test() {\n" +
" AAAAAA.b()\n" +
".c()\n" +
".d()\n" +
" /* simple block comment */\n" +
".e();\n" +
" }\n" +
"}",
"class X {\n" +
" public void test() {\n" +
" AAAAAA.b()\n" +
" .c()\n" +
" .d()\n" +
" /* simple block comment */\n" +
" .e();\n" +
" }\n" +
"}");
}
public void testMultipleMethodAnnotationsCommentedInTheMiddle() {
getSettings().BLANK_LINES_AFTER_CLASS_HEADER = 1;
getSettings().getRootSettings().getIndentOptions(JavaFileType.INSTANCE).INDENT_SIZE = 4;
// Inspired by IDEA-53942
doTextTest(
"public class Test {\n" +
" @Override\n" +
"// @XmlElement(name = \"Document\", required = true, type = DocumentType.class)\n" +
" @XmlTransient\n" +
" void foo() {\n" +
"}\n" +
"}",
"public class Test {\n" +
"\n" +
" @Override\n" +
"// @XmlElement(name = \"Document\", required = true, type = DocumentType.class)\n" +
" @XmlTransient\n" +
" void foo() {\n" +
" }\n" +
"}"
);
}
public void testTernaryOperator() {
// Inspired by IDEADEV-13018
getSettings().ALIGN_MULTILINE_TERNARY_OPERATION = true;
doMethodTest("int i = a ? x\n" + ": y;", "int i = a ? x\n" + " : y;");
}
public void testMethodCallArgumentsAndSmartTabs() throws IncorrectOperationException {
// Inspired by IDEADEV-20144.
getSettings().ALIGN_MULTILINE_PARAMETERS_IN_CALLS = true;
getSettings().getRootSettings().getIndentOptions(JavaFileType.INSTANCE).SMART_TABS = true;
getSettings().getRootSettings().getIndentOptions(JavaFileType.INSTANCE).USE_TAB_CHARACTER = true;
doTextTest("class Foo {\n" +
" void foo() {\n" +
" bar(new Object[] {\n" +
" \"hello1\",\n" +
" \"hello2\", add(\"hello3\",\n" +
" \"world\")\n" +
"});" +
" }}", "class Foo {\n" +
"\tvoid foo() {\n" +
"\t\tbar(new Object[]{\n" +
"\t\t\t\t\"hello1\",\n" +
"\t\t\t\t\"hello2\", add(\"hello3\",\n" +
"\t\t\t\t \"world\")\n" +
"\t\t});\n" +
"\t}\n" +
"}");
}
public void testArrayInitializer() throws IncorrectOperationException {
// Inspired by IDEADEV-16136
getSettings().ARRAY_INITIALIZER_WRAP = CommonCodeStyleSettings.WRAP_ALWAYS;
getSettings().ALIGN_MULTILINE_ARRAY_INITIALIZER_EXPRESSION = true;
doTextTest(
"@SuppressWarnings({\"UseOfSystemOutOrSystemErr\", \"AssignmentToCollectionOrArrayFieldFromParameter\", \"ReturnOfCollectionOrArrayField\"})\n" +
"public class Some {\n" +
"}",
"@SuppressWarnings({\"UseOfSystemOutOrSystemErr\",\n" +
" \"AssignmentToCollectionOrArrayFieldFromParameter\",\n" +
" \"ReturnOfCollectionOrArrayField\"})\n" +
"public class Some {\n" +
"}");
}
public void testMethodBrackets() {
// Inspired by IDEA-53013
getSettings().ALIGN_MULTILINE_METHOD_BRACKETS = true;
getSettings().ALIGN_MULTILINE_PARENTHESIZED_EXPRESSION = false;
getSettings().ALIGN_MULTILINE_PARAMETERS = true;
getSettings().ALIGN_MULTILINE_PARAMETERS_IN_CALLS = true;
getSettings().CALL_PARAMETERS_RPAREN_ON_NEXT_LINE = true;
getSettings().METHOD_PARAMETERS_RPAREN_ON_NEXT_LINE = true;
doClassTest(
"public void foo(int i,\n" +
" int j) {\n" +
"}\n" +
"\n" +
" public void bar() {\n" +
" foo(1,\n" +
" 2);\n" +
" }",
"public void foo(int i,\n" +
" int j\n" +
" ) {\n" +
"}\n" +
"\n" +
"public void bar() {\n" +
" foo(1,\n" +
" 2\n" +
" );\n" +
"}"
);
// Inspired by IDEA-55306
getSettings().ALIGN_MULTILINE_METHOD_BRACKETS = false;
getSettings().CALL_PARAMETERS_RPAREN_ON_NEXT_LINE = false;
String method =
"executeCommand(new Command<Boolean>() {\n" +
" public Boolean run() throws ExecutionException {\n" +
" return doInterrupt();\n" +
" }\n" +
"});";
doMethodTest(method, method);
}
public void testFieldInColumnsAlignment() {
// Inspired by IDEA-55147
getSettings().ALIGN_GROUP_FIELD_DECLARATIONS = true;
getSettings().FIELD_ANNOTATION_WRAP = CommonCodeStyleSettings.DO_NOT_WRAP;
getSettings().VARIABLE_ANNOTATION_WRAP = CommonCodeStyleSettings.DO_NOT_WRAP;
doTextTest(
"public class FormattingTest {\n" +
"\n" +
" int start = 1;\n" +
" double end = 2;\n" +
"\n" +
" int i2 = 1;\n" +
" double dd2,\n" +
" dd3 = 2;\n" +
"\n" +
" // asd\n" +
" char ccc3 = 'a';\n" +
" double ddd31, ddd32 = 1;\n" +
"\n" +
" private\n" +
" final String s4 = \"\";\n" +
" private\n" +
" transient int i4 = 1;\n" +
"\n" +
" private final String s5 = \"xxx\";\n" +
" private transient int iiii5 = 1;\n" +
" /*sdf*/\n" +
" @MyAnnotation(value = 1, text = 2) float f5 = 1;\n" +
"}",
"public class FormattingTest {\n" +
"\n" +
" int start = 1;\n" +
" double end = 2;\n" +
"\n" +
" int i2 = 1;\n" +
" double dd2,\n" +
" dd3 = 2;\n" +
"\n" +
" // asd\n" +
" char ccc3 = 'a';\n" +
" double ddd31, ddd32 = 1;\n" +
"\n" +
" private\n" +
" final String s4 = \"\";\n" +
" private\n" +
" transient int i4 = 1;\n" +
"\n" +
" private final String s5 = \"xxx\";\n" +
" private transient int iiii5 = 1;\n" +
" /*sdf*/\n" +
" @MyAnnotation(value = 1, text = 2) float f5 = 1;\n" +
"}"
);
}
public void testTabsAndFieldsInColumnsAlignment() {
// Inspired by IDEA-56242
getSettings().ALIGN_GROUP_FIELD_DECLARATIONS = true;
getIndentOptions().USE_TAB_CHARACTER = true;
doTextTest(
"public class Test {\n" +
"\tprivate Long field2 = null;\n" +
"\tprivate final Object field1 = null;\n" +
"\tprivate int i = 1;\n" +
"}",
"public class Test {\n" +
"\tprivate Long field2 = null;\n" +
"\tprivate final Object field1 = null;\n" +
"\tprivate int i = 1;\n" +
"}"
);
}
public void testDoNotAlignIfNotEnabled() {
getSettings().ALIGN_GROUP_FIELD_DECLARATIONS = false;
doTextTest(
"public class Test {\n" +
"private Long field2 = null;\n" +
"private final Object field1 = null;\n" +
"private int i = 1;\n" +
"}",
"public class Test {\n" +
" private Long field2 = null;\n" +
" private final Object field1 = null;\n" +
" private int i = 1;\n" +
"}"
);
}
public void testAnnotatedAndNonAnnotatedFieldsInColumnsAlignment() {
// Inspired by IDEA-60237
getSettings().ALIGN_GROUP_FIELD_DECLARATIONS = true;
doTextTest(
"public class Test {\n" +
" @Id\n" +
" private final String name;\n" +
" @Column(length = 2 * 1024 * 1024 /* 2 MB */)\n" +
" private String value;\n" +
" private boolean required;\n" +
" private String unsetValue;\n" +
"}",
"public class Test {\n" +
" @Id\n" +
" private final String name;\n" +
" @Column(length = 2 * 1024 * 1024 /* 2 MB */)\n" +
" private String value;\n" +
" private boolean required;\n" +
" private String unsetValue;\n" +
"}"
);
}
public void testAlignThrowsKeyword() {
// Inspired by IDEA-63820
getSettings().ALIGN_THROWS_KEYWORD = true;
doClassTest(
"public void test()\n" +
" throws Exception {}",
"public void test()\n" +
"throws Exception {\n" +
"}"
);
getSettings().ALIGN_THROWS_KEYWORD = false;
doClassTest(
"public void test()\n" +
" throws Exception {}",
"public void test()\n" +
" throws Exception {\n" +
"}"
);
}
public void testAlignResourceList() {
getSettings().KEEP_SIMPLE_BLOCKS_IN_ONE_LINE = true;
getSettings().ALIGN_MULTILINE_RESOURCES = true;
doMethodTest("try (MyResource r1 = null;\n" +
"MyResource r2 = null) { }",
"try (MyResource r1 = null;\n" +
" MyResource r2 = null) { }");
getSettings().ALIGN_MULTILINE_RESOURCES = false;
doMethodTest("try (MyResource r1 = null;\n" +
"MyResource r2 = null) { }",
"try (MyResource r1 = null;\n" +
" MyResource r2 = null) { }");
}
public void testChainedMethodCallsAfterFieldsChain_WithAlignment() {
getSettings().ALIGN_MULTILINE_CHAINED_METHODS = true;
getSettings().METHOD_CALL_CHAIN_WRAP = CommonCodeStyleSettings.WRAP_ALWAYS;
doMethodTest(
"a.current.current.current.getThis().getThis().getThis();",
"a.current.current.current.getThis()\n" +
" .getThis()\n" +
" .getThis();"
);
doMethodTest(
"a.current.current.current.getThis().getThis().getThis().current.getThis().getThis().getThis().getThis();",
"a.current.current.current.getThis()\n" +
" .getThis()\n" +
" .getThis().current.getThis()\n" +
" .getThis()\n" +
" .getThis()\n" +
" .getThis();"
);
String onlyMethodCalls = "getThis().getThis().getThis();";
String formatedMethodCalls = "getThis().getThis()\n" +
" .getThis();";
doMethodTest(onlyMethodCalls, formatedMethodCalls);
}
public void testChainedMethodCallsAfterFieldsChain_WithoutAlignment() {
getSettings().ALIGN_MULTILINE_CHAINED_METHODS = false;
getSettings().METHOD_CALL_CHAIN_WRAP = CommonCodeStyleSettings.WRAP_ALWAYS;
doMethodTest(
"a.current.current.current.getThis().getThis().getThis();",
"a.current.current.current.getThis()\n" +
" .getThis()\n" +
" .getThis();"
);
}
public void testChainedMethodCalls_WithChopDownIfLongOption() {
getSettings().ALIGN_MULTILINE_CHAINED_METHODS = true;
getSettings().METHOD_CALL_CHAIN_WRAP = CommonCodeStyleSettings.WRAP_ON_EVERY_ITEM; // it's equal to "Chop down if long"
getSettings().RIGHT_MARGIN = 50;
String before = "a.current.current.getThis().getThis().getThis().getThis().getThis();";
doMethodTest(
before,
"a.current.current.getThis()\n" +
" .getThis()\n" +
" .getThis()\n" +
" .getThis()\n" +
" .getThis();"
);
getSettings().RIGHT_MARGIN = 80;
doMethodTest(before, before);
}
public void testChainedMethodCalls_WithWrapIfNeededOption() {
getSettings().ALIGN_MULTILINE_CHAINED_METHODS = false;
getSettings().METHOD_CALL_CHAIN_WRAP = CommonCodeStyleSettings.WRAP_AS_NEEDED;
getSettings().RIGHT_MARGIN = 50;
String before = "a.current.current.getThis().getThis().getThis().getThis();";
doMethodTest(
before,
"a.current.current.getThis().getThis()\n" +
" .getThis().getThis();"
);
getSettings().ALIGN_MULTILINE_CHAINED_METHODS = true;
doMethodTest(
before,
"a.current.current.getThis().getThis()\n" +
" .getThis().getThis();"
);
getSettings().RIGHT_MARGIN = 75;
doMethodTest(before, before);
}
public void testAlignMethodCalls_PassedAsParameters_InMethodCall() {
getSettings().ALIGN_MULTILINE_PARAMETERS_IN_CALLS = true;
doMethodTest(
"test(call1(),\n" +
" call2(),\n" +
" call3());\n",
"test(call1(),\n" +
" call2(),\n" +
" call3());\n"
);
}
public void testLocalVariablesAlignment() {
getSettings().ALIGN_CONSECUTIVE_VARIABLE_DECLARATIONS = true;
doMethodTest(
"int a = 2;\n" +
"String myString = \"my string\"",
"int a = 2;\n" +
"String myString = \"my string\""
);
}
public void testAlignOnlyDeclarationStatements() {
getSettings().ALIGN_CONSECUTIVE_VARIABLE_DECLARATIONS = true;
doMethodTest(
" String s;\n" +
" int a = 2;\n" +
"s = \"abs\";\n" +
"long stamp = 12;",
"String s;\n" +
"int a = 2;\n" +
"s = \"abs\";\n" +
"long stamp = 12;"
);
}
public void testAlignFieldDeclarations() {
getSettings().ALIGN_GROUP_FIELD_DECLARATIONS = true;
doClassTest(
"char a = '2';\n" +
"int aaaaa = 3;\n" +
"String b;",
"char a = '2';\n" +
"int aaaaa = 3;\n" +
"String b;");
}
public void testAlignVarDeclarations() {
getSettings().ALIGN_CONSECUTIVE_VARIABLE_DECLARATIONS = true;
doMethodTest(
"char a = '2';\n" +
"int aaaaa = 3;\n" +
"String b;",
"char a = '2';\n" +
"int aaaaa = 3;\n" +
"String b;");
}
public void testDoNotAlignWhenBlankLine() {
getSettings().ALIGN_CONSECUTIVE_VARIABLE_DECLARATIONS = true;
doMethodTest(
"int a = 2;\n" +
"\n" +
"String myString = \"my string\"",
"int a = 2;\n" +
"\n" +
"String myString = \"my string\""
);
}
public void testDoNotAlignWhenGroupInterrupted() {
getSettings().ALIGN_CONSECUTIVE_VARIABLE_DECLARATIONS = true;
doMethodTest(
"int a = 2;\n" +
"System.out.println(\"hi!\")\n" +
"String myString = \"my string\"",
"int a = 2;\n" +
"System.out.println(\"hi!\")\n" +
"String myString = \"my string\""
);
}
public void testDoNotAlignMultiDeclarations() {
getSettings().ALIGN_CONSECUTIVE_VARIABLE_DECLARATIONS = true;
doMethodTest(
" int a, b = 2;\n" +
"String myString = \"my string\"",
"int a, b = 2;\n" +
"String myString = \"my string\""
);
}
public void testDoNotAlignMultilineParams() {
getSettings().ALIGN_CONSECUTIVE_VARIABLE_DECLARATIONS = true;
doMethodTest(
"int a = 12;\n" +
" Runnable runnable = new Runnable() {\n" +
" @Override\n" +
" public void run() {\n" +
" System.out.println(\"AAA!\");\n" +
" }\n" +
"};",
"int a = 12;\n" +
"Runnable runnable = new Runnable() {\n" +
" @Override\n" +
" public void run() {\n" +
" System.out.println(\"AAA!\");\n" +
" }\n" +
"};"
);
doMethodTest(
" Runnable runnable = new Runnable() {\n" +
" @Override\n" +
" public void run() {\n" +
" System.out.println(\"AAA!\");\n" +
" }\n" +
"};\n" +
"int c = 12;",
"Runnable runnable = new Runnable() {\n" +
" @Override\n" +
" public void run() {\n" +
" System.out.println(\"AAA!\");\n" +
" }\n" +
"};\n" +
"int c = 12;"
);
doMethodTest(
" int ac = 99;\n" +
"Runnable runnable = new Runnable() {\n" +
" @Override\n" +
" public void run() {\n" +
" System.out.println(\"AAA!\");\n" +
" }\n" +
"};\n" +
"int c = 12;",
"int ac = 99;\n" +
"Runnable runnable = new Runnable() {\n" +
" @Override\n" +
" public void run() {\n" +
" System.out.println(\"AAA!\");\n" +
" }\n" +
"};\n" +
"int c = 12;"
);
}
public void testDoNotAlign_IfFirstMultiline() {
getSettings().ALIGN_CONSECUTIVE_VARIABLE_DECLARATIONS = true;
doMethodTest(
"int\n" +
" i = 0;\n" +
"int[] a = new int[]{1, 2, 0x0052, 0x0053, 0x0054};\n" +
"int var1 = 1;\n" +
"int var2 = 2;",
"int\n" +
" i = 0;\n" +
"int[] a = new int[]{1, 2, 0x0052, 0x0053, 0x0054};\n" +
"int var1 = 1;\n" +
"int var2 = 2;"
);
}
public void testAlign_InMethod() {
getSettings().ALIGN_CONSECUTIVE_VARIABLE_DECLARATIONS = true;
doClassTest(
"public void run() {\n" +
"\n" +
" int a = 2;\n" +
" String superString = \"\";\n" +
"\n" +
" test(call1(), call2(), call3());\n" +
" }",
"public void run() {\n" +
"\n" +
" int a = 2;\n" +
" String superString = \"\";\n" +
"\n" +
" test(call1(), call2(), call3());\n" +
"}"
);
doClassTest(
"public void run() {\n" +
"\n" +
" test(call1(), call2(), call3());\n" +
"\n" +
" int a = 2;\n" +
" String superString = \"\";\n" +
"}",
"public void run() {\n" +
"\n" +
" test(call1(), call2(), call3());\n" +
"\n" +
" int a = 2;\n" +
" String superString = \"\";\n" +
"}");
}
public void test_Shift_All_AlignedParameters() {
myLineRange = new TextRange(2, 2);
getSettings().ALIGN_MULTILINE_PARAMETERS_IN_CALLS = true;
doTextTest(
REFORMAT_WITH_CONTEXT,
"public class Test {\n" +
"\n" +
" public void fooooo(String foo,\n" +
" String booo,\n" +
" String kakadoo) {\n" +
"\n" +
" }\n" +
"\n" +
"}",
"public class Test {\n" +
"\n" +
" public void fooooo(String foo,\n" +
" String booo,\n" +
" String kakadoo) {\n" +
"\n" +
" }\n" +
"\n" +
"}"
);
}
public void test_Align_UnselectedField_IfNeeded() {
myLineRange = new TextRange(2, 2);
getSettings().ALIGN_GROUP_FIELD_DECLARATIONS = true;
doTextTest(
REFORMAT_WITH_CONTEXT,
"public class Test {\n" +
" public int i = 1;\n" +
" public String iiiiiiiiii = 2;\n" +
"}",
"public class Test {\n" +
" public int i = 1;\n" +
" public String iiiiiiiiii = 2;\n" +
"}"
);
}
public void test_Align_UnselectedVariable_IfNeeded() {
myLineRange = new TextRange(3, 3);
getSettings().ALIGN_CONSECUTIVE_VARIABLE_DECLARATIONS = true;
doTextTest(
REFORMAT_WITH_CONTEXT,
"public class Test {\n" +
" public void test() {\n" +
" int s = 2;\n" +
" String sssss = 3;\n" +
" }\n" +
"}",
"public class Test {\n" +
" public void test() {\n" +
" int s = 2;\n" +
" String sssss = 3;\n" +
" }\n" +
"}"
);
}
public void test_Align_ConsecutiveVars_InsideIfBlock() {
getSettings().ALIGN_CONSECUTIVE_VARIABLE_DECLARATIONS = true;
doMethodTest(
"if (a > 2) {\n" +
"int a=2;\n" +
"String name=\"Yarik\";\n" +
"}\n",
"if (a > 2) {\n" +
" int a = 2;\n" +
" String name = \"Yarik\";\n" +
"}\n"
);
}
public void test_Align_ConsecutiveVars_InsideForBlock() {
getSettings().ALIGN_CONSECUTIVE_VARIABLE_DECLARATIONS = true;
doMethodTest(
" for (int i = 0; i < 10; i++) {\n" +
" int a=2;\n" +
" String name=\"Xa\";\n" +
" }\n",
"for (int i = 0; i < 10; i++) {\n" +
" int a = 2;\n" +
" String name = \"Xa\";\n" +
"}\n"
);
}
public void test_Align_ConsecutiveVars_InsideTryBlock() {
getSettings().ALIGN_CONSECUTIVE_VARIABLE_DECLARATIONS = true;
doMethodTest(
" try {\n" +
" int x = getX();\n" +
" String name = \"Ha\";\n" +
" }\n" +
" catch (IOException exception) {\n" +
" int y = 12;\n" +
" String test = \"Test\";\n" +
" }\n" +
" finally {\n" +
" int z = 12;\n" +
" String zzzz = \"pnmhd\";\n" +
" }\n",
"try {\n" +
" int x = getX();\n" +
" String name = \"Ha\";\n" +
"} catch (IOException exception) {\n" +
" int y = 12;\n" +
" String test = \"Test\";\n" +
"} finally {\n" +
" int z = 12;\n" +
" String zzzz = \"pnmhd\";\n" +
"}\n"
);
}
public void test_Align_ConsecutiveVars_InsideCodeBlock() {
getSettings().ALIGN_CONSECUTIVE_VARIABLE_DECLARATIONS = true;
doMethodTest(
" System.out.println(\"AAAA\");\n" +
" int a = 2;\n" +
" \n" +
" {\n" +
" int x=2;\n" +
" String name=3;\n" +
" }\n",
"System.out.println(\"AAAA\");\n" +
"int a = 2;\n" +
"\n" +
"{\n" +
" int x = 2;\n" +
" String name = 3;\n" +
"}\n"
);
}
public void test_AlignComments_BetweenChainedMethodCalls() {
getSettings().ALIGN_MULTILINE_CHAINED_METHODS = true;
doMethodTest(
"ActionBarPullToRefresh.from(getActivity())\n" +
" // Mark the ListView as pullable\n" +
" .theseChildrenArePullable(eventsListView)\n" +
" // Set the OnRefreshListener\n" +
" .listener(this)\n" +
" // Use the AbsListView delegate for StickyListHeadersListView\n" +
" .useViewDelegate(StickyListHeadersListView.class, new AbsListViewDelegate())\n" +
" // Finally commit the setup to our PullToRefreshLayout\n" +
" .setup(mPullToRefreshLayout);",
"ActionBarPullToRefresh.from(getActivity())\n" +
" // Mark the ListView as pullable\n" +
" .theseChildrenArePullable(eventsListView)\n" +
" // Set the OnRefreshListener\n" +
" .listener(this)\n" +
" // Use the AbsListView delegate for StickyListHeadersListView\n" +
" .useViewDelegate(StickyListHeadersListView.class, new AbsListViewDelegate())\n" +
" // Finally commit the setup to our PullToRefreshLayout\n" +
" .setup(mPullToRefreshLayout);"
);
}
public void test_AlignComments_2() {
getSettings().ALIGN_MULTILINE_CHAINED_METHODS = true;
doClassTest(
"public String returnWithBuilder2() {\n" +
" return MoreObjects\n" +
" .toStringHelper(this)\n" +
" .add(\"value\", value)\n" +
" // comment\n" +
" .toString();\n" +
" }",
"public String returnWithBuilder2() {\n" +
" return MoreObjects\n" +
" .toStringHelper(this)\n" +
" .add(\"value\", value)\n" +
" // comment\n" +
" .toString();\n" +
"}"
);
}
public void test_AlignSubsequentOneLineMethods() {
getSettings().KEEP_SIMPLE_METHODS_IN_ONE_LINE = true;
getSettings().ALIGN_SUBSEQUENT_SIMPLE_METHODS = true;
doTextTest(
"public class Test {\n" +
"\n" +
" public void testSuperDuperFuckerMother() { System.out.println(\"AAA\"); }\n" +
"\n" +
" public void testCounterMounter() { System.out.println(\"XXXX\"); }\n" +
"\n" +
"}",
"public class Test {\n" +
"\n" +
" public void testSuperDuperFuckerMother() { System.out.println(\"AAA\"); }\n" +
"\n" +
" public void testCounterMounter() { System.out.println(\"XXXX\"); }\n" +
"\n" +
"}"
);
}
public void test_alignAssignments() {
getSettings().ALIGN_CONSECUTIVE_ASSIGNMENTS = true;
doTextTest(
"public class Test {\n" +
" void foo(int a, int xyz) {\n" +
" a = 9999;\n" +
" xyz = 1;\n" +
" }\n" +
"}",
"public class Test {\n" +
" void foo(int a, int xyz) {\n" +
" a = 9999;\n" +
" xyz = 1;\n" +
" }\n" +
"}"
);
}
public void test_alignMultilineAssignments() {
getSettings().ALIGN_CONSECUTIVE_ASSIGNMENTS = true;
getSettings().ALIGN_MULTILINE_ASSIGNMENT = true;
doTextTest(
"public class Test {\n" +
" void foo(int a, int xyz) {\n" +
" a = 9999;\n" +
" xyz = a = \n" +
" a = 12;\n" +
" }\n" +
"}",
"public class Test {\n" +
" void foo(int a, int xyz) {\n" +
" a = 9999;\n" +
" xyz = a =\n" +
" a = 12;\n" +
" }\n" +
"}"
);
}
public void test_alignMultilineAssignmentsMixedWithDeclaration() {
getSettings().ALIGN_CONSECUTIVE_ASSIGNMENTS = true;
getSettings().ALIGN_MULTILINE_ASSIGNMENT = true;
getSettings().ALIGN_CONSECUTIVE_VARIABLE_DECLARATIONS = true;
doTextTest(
"public class Test {\n" +
" void foo(int a, int xyz, int bc) {\n" +
" bc = 9999;\n" +
" a = 9999;\n" +
" int basdf = 1234;\n" +
" int as = 3;\n" +
" xyz = a = \n" +
" a = 12;\n" +
" }\n" +
"}",
"public class Test {\n" +
" void foo(int a, int xyz, int bc) {\n" +
" bc = 9999;\n" +
" a = 9999;\n" +
" int basdf = 1234;\n" +
" int as = 3;\n" +
" xyz = a =\n" +
" a = 12;\n" +
" }\n" +
"}"
);
}
public void test_alignAssignmentsFields() {
getSettings().ALIGN_CONSECUTIVE_ASSIGNMENTS = true;
doTextTest(
"public class Test {\n" +
" void foo(A a, int xyz) {\n" +
" a.bar = 9999;\n" +
" xyz = 1;\n" +
" }\n" +
"}",
"public class Test {\n" +
" void foo(A a, int xyz) {\n" +
" a.bar = 9999;\n" +
" xyz = 1;\n" +
" }\n" +
"}"
);
}
public void test_alignMultilineTextBlock() {
getJavaSettings().ALIGN_MULTILINE_TEXT_BLOCKS = true;
doTextTest(
"public class Test {\n" +
" void foo() {\n" +
" String block = \"\"\"\n" +
" text\n" +
" block\n" +
" \"\"\";\n" +
" }\n" +
"}",
"public class Test {\n" +
" void foo() {\n" +
" String block = \"\"\"\n" +
" text\n" +
" block\n" +
" \"\"\";\n" +
" }\n" +
"}"
);
}
@SuppressWarnings("unused")
public void _testIdea199677() {
getSettings().ALIGN_CONSECUTIVE_VARIABLE_DECLARATIONS = true;
getSettings().CALL_PARAMETERS_WRAP = 2;
getSettings().CALL_PARAMETERS_LPAREN_ON_NEXT_LINE = true;
getSettings().CALL_PARAMETERS_RPAREN_ON_NEXT_LINE = true;
doTextTest(
"public class Main {\n" +
"\n" +
" public static void main(String[] args) {\n" +
" int one = 1;\n" +
" int a_million_dollars = 1000000;\n" +
"\n" +
" doSomething(one, a_million_dollars);\n" +
" }\n" +
"\n" +
" private static void doSomething(int one, int two) {\n" +
" }\n" +
"\n" +
"}",
"public class Main {\n" +
"\n" +
" public static void main(String[] args) {\n" +
" int one = 1;\n" +
" int a_million_dollars = 1000000;\n" +
"\n" +
" doSomething(\n" +
" one,\n" +
" a_million_dollars\n" +
" );\n" +
" }\n" +
"\n" +
" private static void doSomething(int one, int two) {\n" +
" }\n" +
"\n" +
"}"
);
}
} | dahlstrom-g/intellij-community | java/java-tests/testSrc/com/intellij/java/psi/formatter/java/JavaFormatterAlignmentTest.java | Java | apache-2.0 | 30,615 |
// -*- mode: java; c-basic-offset: 2; -*-
// Copyright 2009-2011 Google, All Rights reserved
// Copyright 2011-2012 MIT, All rights reserved
// Released under the Apache License, Version 2.0
// http://www.apache.org/licenses/LICENSE-2.0
package com.google.appinventor.server;
import com.google.appinventor.server.storage.StorageIo;
import com.google.appinventor.server.storage.StorageIoInstanceHolder;
import com.google.appinventor.server.storage.UnauthorizedAccessException;
import com.google.appinventor.shared.rpc.project.Project;
import com.google.appinventor.shared.rpc.project.ProjectSourceZip;
import com.google.appinventor.shared.rpc.project.RawFile;
import com.google.appinventor.shared.rpc.project.TextFile;
import com.google.appinventor.shared.storage.StorageUtil;
import com.google.common.io.ByteStreams;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
/**
* Tests for {@link FileExporterImpl}.
*
*/
public class FileExporterImplTest extends LocalDatastoreTestCase {
private static final String USER_ID = "1";
// The following represent a fake project, containing both source and
// output files, for the purpose of testing.
private static final String FAKE_PROJECT_TYPE = "FakeProjectType";
private static final String PROJECT_NAME = "Project1";
private static final String FORM1_NAME = "Screen1";
private static final String FORM1_QUALIFIED_NAME = "com.yourdomain." + FORM1_NAME;
private static final String FORM1_CONTENT = "Form A\nEnd Form";
private static final String IMAGE1_NAME = "Image.jpg";
private static final byte[] IMAGE_CONTENT = { (byte) 0, (byte) 1, (byte) 32, (byte) 255};
private static final String TARGET1_NAME = "Project1.apk";
private static final String TARGET1_QUALIFIED_NAME = "build/target1/" + TARGET1_NAME;
private static final byte[] TARGET1_CONTENT = "pk1".getBytes();
private static final String TARGET2_NAME = "Project2.pak";
private static final String TARGET2_QUALIFIED_NAME = "build/target2/" + TARGET2_NAME;
private static final byte[] TARGET2_CONTENT = "pk2".getBytes();
private static final String SETTINGS = "";
private static final String HISTORY = "1:History";
private StorageIo storageIo;
private FileExporterImpl exporter;
private long projectId;
@Override
protected void setUp() throws Exception {
super.setUp();
storageIo = StorageIoInstanceHolder.INSTANCE;
exporter = new FileExporterImpl();
Project project = new Project(PROJECT_NAME);
project.setProjectType(FAKE_PROJECT_TYPE);
project.setProjectHistory(HISTORY);
project.addTextFile(new TextFile(FORM1_QUALIFIED_NAME, ""));
projectId = storageIo.createProject(USER_ID, project, SETTINGS);
storageIo.uploadFile(projectId, FORM1_QUALIFIED_NAME, USER_ID, FORM1_CONTENT,
StorageUtil.DEFAULT_CHARSET);
storageIo.addSourceFilesToProject(USER_ID, projectId, false, IMAGE1_NAME);
storageIo.uploadRawFile(projectId, IMAGE1_NAME, USER_ID, true, IMAGE_CONTENT);
storageIo.addOutputFilesToProject(USER_ID, projectId, TARGET1_QUALIFIED_NAME);
storageIo.uploadRawFile(projectId, TARGET1_QUALIFIED_NAME, USER_ID,
true, TARGET1_CONTENT);
storageIo.addOutputFilesToProject(USER_ID, projectId, TARGET2_QUALIFIED_NAME);
storageIo.uploadRawFile(projectId, TARGET2_QUALIFIED_NAME, USER_ID,
true, TARGET2_CONTENT);
}
private Map<String, byte[]> testExportProjectSourceZipHelper(ProjectSourceZip project)
throws IOException {
ZipInputStream zis =
new ZipInputStream(new ByteArrayInputStream(project.getContent()));
Map<String, byte[]> content = new HashMap<String, byte[]>();
ZipEntry zipEntry;
while ((zipEntry = zis.getNextEntry()) != null) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ByteStreams.copy(zis, baos);
content.put(zipEntry.getName(), baos.toByteArray());
}
assertEquals(content.size(), project.getFileCount());
assertTrue(content.containsKey(FORM1_QUALIFIED_NAME));
assertTrue(content.containsKey(IMAGE1_NAME));
assertFalse(content.containsKey(TARGET1_NAME));
assertEquals(FORM1_CONTENT, new String(content.get(FORM1_QUALIFIED_NAME),
StorageUtil.DEFAULT_CHARSET));
assertTrue(Arrays.equals(IMAGE_CONTENT, content.get(IMAGE1_NAME)));
return content;
}
public void testExportProjectSourceZipWithoutHistory() throws IOException {
ProjectSourceZip project = exporter.exportProjectSourceZip(USER_ID, projectId,
false, false, null);
Map<String, byte[]> content = testExportProjectSourceZipHelper(project);
assertEquals(2, content.size());
/* Do not expect remix history when includeProjectHistory parameter is false
* as in the publish case. */
assertFalse(content.containsKey(FileExporter.REMIX_INFORMATION_FILE_PATH));
}
// TODO(user): Add test with properly formatted history
public void testExportProjectSourceZipWithHistory() throws IOException {
ProjectSourceZip project = exporter.exportProjectSourceZip(USER_ID, projectId,
true, false, null);
Map<String, byte[]> content = testExportProjectSourceZipHelper(project);
assertEquals(3, content.size());
// Expect the remix file to be in
assertTrue(content.containsKey(FileExporter.REMIX_INFORMATION_FILE_PATH));
assertEquals(HISTORY, new String(content.get(FileExporter.REMIX_INFORMATION_FILE_PATH),
StorageUtil.DEFAULT_CHARSET));
}
public void testExportProjectSourceZipWithNonExistingProject() throws IOException {
try {
exporter.exportProjectSourceZip(USER_ID, projectId + 1, false, false, null);
fail();
} catch (Exception e) {
assertTrue(e instanceof IllegalArgumentException ||
e.getCause() instanceof IllegalArgumentException);
}
}
public void testExportProjectOutputFileWithTarget() throws IOException {
RawFile file = exporter.exportProjectOutputFile(USER_ID, projectId, "target1");
assertEquals(TARGET1_NAME, file.getFileName());
assertTrue(Arrays.equals(TARGET1_CONTENT, file.getContent()));
}
public void testExportProjectOutputFileWithNonExistingTraget() throws IOException {
try {
exporter.exportProjectOutputFile(USER_ID, projectId, "target3");
fail();
} catch (IllegalArgumentException e) {
// expected
}
}
public void testExportFile() throws IOException {
RawFile file = exporter.exportFile(USER_ID, projectId, FORM1_QUALIFIED_NAME);
assertEquals(FORM1_QUALIFIED_NAME, file.getFileName());
assertEquals(FORM1_CONTENT, new String(file.getContent(), StorageUtil.DEFAULT_CHARSET));
}
public void testExportFileWithNonExistingFile() throws IOException {
final String nonExistingFileName = FORM1_QUALIFIED_NAME + "1";
try {
exporter.exportFile(USER_ID, projectId, nonExistingFileName);
fail();
} catch (RuntimeException e) {
// expected
// note that FileExporter throws an explicit RuntimeException
}
}
// TODO(user): Add test of exportAllProjectsSourceZip().
}
| kidebit/AudioBlurp | appinventor/appengine/tests/com/google/appinventor/server/FileExporterImplTest.java | Java | apache-2.0 | 7,269 |
/*
* Copyright (c) 2008-2017, Hazelcast, Inc. All Rights Reserved.
*
* 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 com.hazelcast.client.spi;
import com.hazelcast.core.Partition;
import com.hazelcast.nio.Address;
import com.hazelcast.nio.serialization.Data;
/**
* Partition service for Hazelcast clients.
*
* Allows to retrieve information about the partition count, the partition owner or the partitionId of a key.
*/
public interface ClientPartitionService {
Address getPartitionOwner(int partitionId);
int getPartitionId(Data key);
int getPartitionId(Object key);
int getPartitionCount();
Partition getPartition(int partitionId);
}
| tombujok/hazelcast | hazelcast-client/src/main/java/com/hazelcast/client/spi/ClientPartitionService.java | Java | apache-2.0 | 1,185 |
/*
* Copyright 2016 The Netty Project
*
* The Netty Project licenses this file to you 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:
*
* https://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 io.netty.handler.ssl;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.junit.Assume.assumeFalse;
import static org.junit.Assume.assumeTrue;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.security.PrivateKey;
import io.netty.buffer.UnpooledByteBufAllocator;
import org.junit.Test;
import io.netty.handler.ssl.util.SelfSignedCertificate;
import io.netty.util.ReferenceCountUtil;
public class PemEncodedTest {
@Test
public void testPemEncodedOpenSsl() throws Exception {
testPemEncoded(SslProvider.OPENSSL);
}
@Test
public void testPemEncodedOpenSslRef() throws Exception {
testPemEncoded(SslProvider.OPENSSL_REFCNT);
}
private static void testPemEncoded(SslProvider provider) throws Exception {
assumeTrue(OpenSsl.isAvailable());
assumeFalse(OpenSsl.useKeyManagerFactory());
PemPrivateKey pemKey;
PemX509Certificate pemCert;
SelfSignedCertificate ssc = new SelfSignedCertificate();
try {
pemKey = PemPrivateKey.valueOf(toByteArray(ssc.privateKey()));
pemCert = PemX509Certificate.valueOf(toByteArray(ssc.certificate()));
} finally {
ssc.delete();
}
SslContext context = SslContextBuilder.forServer(pemKey, pemCert)
.sslProvider(provider)
.build();
assertEquals(1, pemKey.refCnt());
assertEquals(1, pemCert.refCnt());
try {
assertTrue(context instanceof ReferenceCountedOpenSslContext);
} finally {
ReferenceCountUtil.release(context);
assertRelease(pemKey);
assertRelease(pemCert);
}
}
@Test(expected = IllegalArgumentException.class)
public void testEncodedReturnsNull() throws Exception {
PemPrivateKey.toPEM(UnpooledByteBufAllocator.DEFAULT, true, new PrivateKey() {
@Override
public String getAlgorithm() {
return null;
}
@Override
public String getFormat() {
return null;
}
@Override
public byte[] getEncoded() {
return null;
}
});
}
private static void assertRelease(PemEncoded encoded) {
assertTrue(encoded.release());
}
private static byte[] toByteArray(File file) throws Exception {
FileInputStream in = new FileInputStream(file);
try {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
try {
byte[] buf = new byte[1024];
int len;
while ((len = in.read(buf)) != -1) {
baos.write(buf, 0, len);
}
} finally {
baos.close();
}
return baos.toByteArray();
} finally {
in.close();
}
}
}
| zer0se7en/netty | handler/src/test/java/io/netty/handler/ssl/PemEncodedTest.java | Java | apache-2.0 | 3,656 |
/*
* Licensed to Metamarkets Group Inc. (Metamarkets) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. Metamarkets licenses this file
* to you 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 io.druid.query.filter;
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.google.common.base.Preconditions;
import com.google.common.base.Predicate;
import com.google.common.base.Predicates;
import com.google.common.base.Strings;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Range;
import com.google.common.collect.RangeSet;
import com.google.common.collect.TreeRangeSet;
import com.google.common.primitives.Floats;
import io.druid.common.guava.GuavaUtils;
import io.druid.java.util.common.StringUtils;
import io.druid.query.extraction.ExtractionFn;
import io.druid.segment.filter.DimensionPredicateFilter;
import io.druid.segment.filter.SelectorFilter;
import java.nio.ByteBuffer;
import java.util.Objects;
/**
*/
public class SelectorDimFilter implements DimFilter
{
private final String dimension;
private final String value;
private final ExtractionFn extractionFn;
private final Object initLock = new Object();
private DruidLongPredicate longPredicate;
private DruidFloatPredicate floatPredicate;
@JsonCreator
public SelectorDimFilter(
@JsonProperty("dimension") String dimension,
@JsonProperty("value") String value,
@JsonProperty("extractionFn") ExtractionFn extractionFn
)
{
Preconditions.checkArgument(dimension != null, "dimension must not be null");
this.dimension = dimension;
this.value = Strings.nullToEmpty(value);
this.extractionFn = extractionFn;
}
@Override
public byte[] getCacheKey()
{
byte[] dimensionBytes = StringUtils.toUtf8(dimension);
byte[] valueBytes = (value == null) ? new byte[]{} : StringUtils.toUtf8(value);
byte[] extractionFnBytes = extractionFn == null ? new byte[0] : extractionFn.getCacheKey();
return ByteBuffer.allocate(3 + dimensionBytes.length + valueBytes.length + extractionFnBytes.length)
.put(DimFilterUtils.SELECTOR_CACHE_ID)
.put(dimensionBytes)
.put(DimFilterUtils.STRING_SEPARATOR)
.put(valueBytes)
.put(DimFilterUtils.STRING_SEPARATOR)
.put(extractionFnBytes)
.array();
}
@Override
public DimFilter optimize()
{
return new InDimFilter(dimension, ImmutableList.of(value), extractionFn).optimize();
}
@Override
public Filter toFilter()
{
if (extractionFn == null) {
return new SelectorFilter(dimension, value);
} else {
final String valueOrNull = Strings.emptyToNull(value);
final DruidPredicateFactory predicateFactory = new DruidPredicateFactory()
{
@Override
public Predicate<String> makeStringPredicate()
{
return Predicates.equalTo(valueOrNull);
}
@Override
public DruidLongPredicate makeLongPredicate()
{
initLongPredicate();
return longPredicate;
}
@Override
public DruidFloatPredicate makeFloatPredicate()
{
initFloatPredicate();
return floatPredicate;
}
};
return new DimensionPredicateFilter(dimension, predicateFactory, extractionFn);
}
}
@JsonProperty
public String getDimension()
{
return dimension;
}
@JsonProperty
public String getValue()
{
return value;
}
@JsonProperty
public ExtractionFn getExtractionFn()
{
return extractionFn;
}
@Override
public String toString()
{
if (extractionFn != null) {
return String.format("%s(%s) = %s", extractionFn, dimension, value);
} else {
return String.format("%s = %s", dimension, value);
}
}
@Override
public boolean equals(Object o)
{
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
SelectorDimFilter that = (SelectorDimFilter) o;
if (!dimension.equals(that.dimension)) {
return false;
}
if (value != null ? !value.equals(that.value) : that.value != null) {
return false;
}
return extractionFn != null ? extractionFn.equals(that.extractionFn) : that.extractionFn == null;
}
@Override
public RangeSet<String> getDimensionRangeSet(String dimension)
{
if (!Objects.equals(getDimension(), dimension) || getExtractionFn() != null) {
return null;
}
RangeSet<String> retSet = TreeRangeSet.create();
retSet.add(Range.singleton(Strings.nullToEmpty(value)));
return retSet;
}
@Override
public int hashCode()
{
int result = dimension.hashCode();
result = 31 * result + (value != null ? value.hashCode() : 0);
result = 31 * result + (extractionFn != null ? extractionFn.hashCode() : 0);
return result;
}
private void initLongPredicate()
{
if (longPredicate != null) {
return;
}
synchronized (initLock) {
if (longPredicate != null) {
return;
}
final Long valueAsLong = GuavaUtils.tryParseLong(value);
if (valueAsLong == null) {
longPredicate = DruidLongPredicate.ALWAYS_FALSE;
} else {
// store the primitive, so we don't unbox for every comparison
final long unboxedLong = valueAsLong.longValue();
longPredicate = new DruidLongPredicate()
{
@Override
public boolean applyLong(long input)
{
return input == unboxedLong;
}
};
}
}
}
private void initFloatPredicate()
{
if (floatPredicate != null) {
return;
}
synchronized (initLock) {
if (floatPredicate != null) {
return;
}
final Float valueAsFloat = Floats.tryParse(value);
if (valueAsFloat == null) {
floatPredicate = DruidFloatPredicate.ALWAYS_FALSE;
} else {
final int floatBits = Float.floatToIntBits(valueAsFloat);
floatPredicate = new DruidFloatPredicate()
{
@Override
public boolean applyFloat(float input)
{
return Float.floatToIntBits(input) == floatBits;
}
};
}
}
}
}
| zhihuij/druid | processing/src/main/java/io/druid/query/filter/SelectorDimFilter.java | Java | apache-2.0 | 6,989 |
/*
* Copyright (C) 2014 The Android Open Source Project
*
* 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 com.android.location.provider;
import android.annotation.NonNull;
import android.annotation.Nullable;
import android.hardware.location.IActivityRecognitionHardware;
import android.hardware.location.IActivityRecognitionHardwareWatcher;
import android.os.Binder;
import android.os.IBinder;
import android.os.Process;
import android.os.RemoteException;
import android.util.Log;
/**
* A watcher class for Activity-Recognition instances.
*
* @deprecated use {@link ActivityRecognitionProviderClient} instead.
*/
@Deprecated
public class ActivityRecognitionProviderWatcher {
private static final String TAG = "ActivityRecognitionProviderWatcher";
private static ActivityRecognitionProviderWatcher sWatcher;
private static final Object sWatcherLock = new Object();
private ActivityRecognitionProvider mActivityRecognitionProvider;
private ActivityRecognitionProviderWatcher() {}
public static ActivityRecognitionProviderWatcher getInstance() {
synchronized (sWatcherLock) {
if (sWatcher == null) {
sWatcher = new ActivityRecognitionProviderWatcher();
}
return sWatcher;
}
}
private IActivityRecognitionHardwareWatcher.Stub mWatcherStub =
new IActivityRecognitionHardwareWatcher.Stub() {
@Override
public void onInstanceChanged(IActivityRecognitionHardware instance) {
int callingUid = Binder.getCallingUid();
if (callingUid != Process.SYSTEM_UID) {
Log.d(TAG, "Ignoring calls from non-system server. Uid: " + callingUid);
return;
}
try {
mActivityRecognitionProvider = new ActivityRecognitionProvider(instance);
} catch (RemoteException e) {
Log.e(TAG, "Error creating Hardware Activity-Recognition", e);
}
}
};
/**
* Gets the binder needed to interact with proxy provider in the platform.
*/
@NonNull
public IBinder getBinder() {
return mWatcherStub;
}
/**
* Gets an object that supports the functionality of {@link ActivityRecognitionProvider}.
*
* @return Non-null value if the functionality is supported by the platform, false otherwise.
*/
@Nullable
public ActivityRecognitionProvider getActivityRecognitionProvider() {
return mActivityRecognitionProvider;
}
}
| Ant-Droid/android_frameworks_base_OLD | location/lib/java/com/android/location/provider/ActivityRecognitionProviderWatcher.java | Java | apache-2.0 | 3,055 |
/*
* Copyright (c) 2008-2017, Hazelcast, Inc. All Rights Reserved.
*
* 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 com.hazelcast.internal.adapter;
import javax.cache.processor.EntryProcessor;
import javax.cache.processor.EntryProcessorException;
import javax.cache.processor.MutableEntry;
import java.io.Serializable;
public class ICacheReplaceEntryProcessor implements EntryProcessor<Integer, String, String>, Serializable {
private static final long serialVersionUID = -396575576353368113L;
@Override
public String process(MutableEntry<Integer, String> entry, Object... arguments) throws EntryProcessorException {
String value = entry.getValue();
if (value == null) {
return null;
}
String oldString = (String) arguments[0];
String newString = (String) arguments[1];
String result = value.replace(oldString, newString);
entry.setValue(result);
return result;
}
}
| tombujok/hazelcast | hazelcast/src/test/java/com/hazelcast/internal/adapter/ICacheReplaceEntryProcessor.java | Java | apache-2.0 | 1,482 |
/*<license>
Copyright 2005 - $Date$ by PeopleWare n.v..
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.
</license>*/
package org.ppwcode.vernacular.persistence_III;
import static org.ppwcode.metainfo_I.License.Type.APACHE_V2;
import java.io.Serializable;
import org.ppwcode.metainfo_I.Copyright;
import org.ppwcode.metainfo_I.License;
import org.ppwcode.metainfo_I.vcs.SvnInfo;
import org.ppwcode.vernacular.semantics_VI.bean.AbstractRousseauBean;
/**
* A partial implementation of the interface {@link PersistentBean}.
*
* @author Nele Smeets
* @author Ruben Vandeginste
* @author Jan Dockx
* @author PeopleWare n.v.
*
* @mudo We now have a dependency here on JPA via annotations. Also, the listener is defined in a subpackage, which
* depends on this package. This introduces a cycle! This is a bad idea. Like this, you always need the JPA
* libraries, even if they are annotations, because the annotations are loaded in the import statements too
* (at least under 1.5). Thus, the annotations must go, and we need to use the xml files.
*/
@Copyright("2004 - $Date$, PeopleWare n.v.")
@License(APACHE_V2)
@SvnInfo(revision = "$Revision$",
date = "$Date$")
public abstract class AbstractPersistentBean<_Id_ extends Serializable> extends AbstractRousseauBean
implements PersistentBean<_Id_> {
/*<property name="id">*/
//------------------------------------------------------------------
public final _Id_ getPersistenceId() {
return $persistenceId;
}
public final boolean hasSamePersistenceId(final PersistentBean<_Id_> other) {
return (other != null) && ((getPersistenceId() == null) ? other.getPersistenceId() == null : getPersistenceId().equals(other.getPersistenceId()));
}
public final void setPersistenceId(final _Id_ persistenceId) {
$persistenceId = persistenceId;
}
// @Id
// @GeneratedValue
// @Column(name="persistenceId")
private _Id_ $persistenceId;
/*</property>*/
}
| jandppw/ppwcode-recovered-from-google-code | java/vernacular/persistence/dev/d20081014-1359/src/main/java/org/ppwcode/vernacular/persistence_III/AbstractPersistentBean.java | Java | apache-2.0 | 2,465 |
/*
* Copyright Terracotta, Inc.
*
* 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.ehcache.config.builders;
import org.ehcache.CacheManager;
import org.ehcache.PersistentCacheManager;
import org.ehcache.config.Builder;
import org.ehcache.config.CacheConfiguration;
import org.ehcache.config.Configuration;
import org.ehcache.config.units.MemoryUnit;
import org.ehcache.core.EhcacheManager;
import org.ehcache.core.spi.store.heap.SizeOfEngine;
import org.ehcache.impl.config.copy.DefaultCopyProviderConfiguration;
import org.ehcache.impl.config.event.CacheEventDispatcherFactoryConfiguration;
import org.ehcache.impl.config.loaderwriter.writebehind.WriteBehindProviderConfiguration;
import org.ehcache.impl.config.persistence.CacheManagerPersistenceConfiguration;
import org.ehcache.impl.config.serializer.DefaultSerializationProviderConfiguration;
import org.ehcache.impl.config.store.heap.DefaultSizeOfEngineProviderConfiguration;
import org.ehcache.impl.config.store.disk.OffHeapDiskStoreProviderConfiguration;
import org.ehcache.spi.copy.Copier;
import org.ehcache.spi.serialization.Serializer;
import org.ehcache.spi.service.Service;
import org.ehcache.spi.service.ServiceCreationConfiguration;
import java.io.File;
import java.util.Collection;
import java.util.HashSet;
import java.util.Set;
import static java.util.Collections.emptySet;
import static java.util.Collections.unmodifiableSet;
import static org.ehcache.config.builders.ConfigurationBuilder.newConfigurationBuilder;
import static org.ehcache.impl.config.store.heap.DefaultSizeOfEngineConfiguration.DEFAULT_MAX_OBJECT_SIZE;
import static org.ehcache.impl.config.store.heap.DefaultSizeOfEngineConfiguration.DEFAULT_OBJECT_GRAPH_SIZE;
import static org.ehcache.impl.config.store.heap.DefaultSizeOfEngineConfiguration.DEFAULT_UNIT;
/**
* The {@code CacheManagerBuilder} enables building cache managers using a fluent style.
* <p>
* As with all Ehcache builders, all instances are immutable and calling any method on the builder will return a new
* instance without modifying the one on which the method was called.
* This enables the sharing of builder instances without any risk of seeing them modified by code elsewhere.
*/
public class CacheManagerBuilder<T extends CacheManager> implements Builder<T> {
private final ConfigurationBuilder configBuilder;
private final Set<Service> services;
/**
* Builds a {@link CacheManager} or a subtype of it and initializes it if requested.
*
* @param init whether the returned {@code CacheManager} is to be initialized or not
* @return a {@code CacheManager} or a subtype of it
*/
public T build(final boolean init) {
final T cacheManager = newCacheManager(services, configBuilder.build());
if(init) {
cacheManager.init();
}
return cacheManager;
}
/**
* Builds a {@link CacheManager} or a subtype of it uninitialized.
*
* @return a {@code CacheManager} or a subtype of it uninitialized
*/
@Override
public T build() {
return build(false);
}
private CacheManagerBuilder() {
this.configBuilder = newConfigurationBuilder();
this.services = emptySet();
}
private CacheManagerBuilder(CacheManagerBuilder<T> builder, Set<Service> services) {
this.configBuilder = builder.configBuilder;
this.services = unmodifiableSet(services);
}
private CacheManagerBuilder(CacheManagerBuilder<T> builder, ConfigurationBuilder configBuilder) {
this.configBuilder = configBuilder;
this.services = builder.services;
}
/**
* Creates a new {@link CacheManager} based on the provided configuration.
* The returned {@code CacheManager} is uninitialized.
*
* @param configuration the configuration to use
* @return a {@code CacheManager}
*/
public static CacheManager newCacheManager(final Configuration configuration) {
return new EhcacheManager(configuration);
}
T newCacheManager(Collection<Service> services, final Configuration configuration) {
final EhcacheManager ehcacheManager = new EhcacheManager(configuration, services);
return cast(ehcacheManager);
}
@SuppressWarnings("unchecked")
T cast(EhcacheManager ehcacheManager) {
return (T) ehcacheManager;
}
/**
* Adds a {@link CacheConfiguration} linked to the specified alias to the returned builder.
*
* @param alias the cache alias
* @param configuration the {@code CacheConfiguration}
* @param <K> the cache key type
* @param <V> the cache value type
* @return a new builder with the added cache configuration
*
* @see CacheConfigurationBuilder
*/
public <K, V> CacheManagerBuilder<T> withCache(String alias, CacheConfiguration<K, V> configuration) {
return new CacheManagerBuilder<>(this, configBuilder.addCache(alias, configuration));
}
/**
* Convenience method to add a {@link CacheConfiguration} linked to the specified alias to the returned builder by
* building it from the provided {@link Builder}.
*
* @param alias the cache alias
* @param configurationBuilder the {@code Builder} to get {@code CacheConfiguration} from
* @param <K> the cache key type
* @param <V> the cache value type
* @return a new builder with the added cache configuration
*
* @see CacheConfigurationBuilder
*/
public <K, V> CacheManagerBuilder<T> withCache(String alias, Builder<? extends CacheConfiguration<K, V>> configurationBuilder) {
return withCache(alias, configurationBuilder.build());
}
/**
* Specializes the returned {@link CacheManager} subtype through a specific {@link CacheManagerConfiguration} which
* will optionally add configurations to the returned builder.
*
* @param cfg the {@code CacheManagerConfiguration} to use
* @param <N> the subtype of {@code CacheManager}
* @return a new builder ready to build a more specific subtype of cache manager
*
* @see #persistence(String)
* @see PersistentCacheManager
* @see CacheManagerPersistenceConfiguration
*/
public <N extends T> CacheManagerBuilder<N> with(CacheManagerConfiguration<N> cfg) {
return cfg.builder(this);
}
/**
* Convenience method to specialize the returned {@link CacheManager} subtype through a {@link CacheManagerConfiguration}
* built using the provided {@link Builder}.
*
* @param cfgBuilder the {@code Builder} to get the {@code CacheManagerConfiguration} from
* @return a new builder ready to build a more specific subtype of cache manager
*
* @see CacheConfigurationBuilder
*/
public <N extends T> CacheManagerBuilder<N> with(Builder<? extends CacheManagerConfiguration<N>> cfgBuilder) {
return with(cfgBuilder.build());
}
/**
* Adds a {@link Service} instance to the returned builder.
* <p>
* The service instance will be used by the constructed {@link CacheManager}.
*
* @param service the {@code Service} to add
* @return a new builder with the added service
*/
public CacheManagerBuilder<T> using(Service service) {
Set<Service> newServices = new HashSet<>(services);
newServices.add(service);
return new CacheManagerBuilder<>(this, newServices);
}
/**
* Adds a default {@link Copier} for the specified type to the returned builder.
*
* @param clazz the {@code Class} for which the copier is
* @param copier the {@code Copier} instance
* @param <C> the type which can be copied
* @return a new builder with the added default copier
*/
public <C> CacheManagerBuilder<T> withCopier(Class<C> clazz, Class<? extends Copier<C>> copier) {
DefaultCopyProviderConfiguration service = configBuilder.findServiceByClass(DefaultCopyProviderConfiguration.class);
if (service == null) {
service = new DefaultCopyProviderConfiguration();
service.addCopierFor(clazz, copier);
return new CacheManagerBuilder<>(this, configBuilder.addService(service));
} else {
DefaultCopyProviderConfiguration newConfig = new DefaultCopyProviderConfiguration(service);
newConfig.addCopierFor(clazz, copier, true);
return new CacheManagerBuilder<>(this, configBuilder.removeService(service).addService(newConfig));
}
}
/**
* Adds a default {@link Serializer} for the specified type to the returned builder.
*
* @param clazz the {@code Class} for which the serializer is
* @param serializer the {@code Serializer} instance
* @param <C> the type which can be serialized
* @return a new builder with the added default serializer
*/
public <C> CacheManagerBuilder<T> withSerializer(Class<C> clazz, Class<? extends Serializer<C>> serializer) {
DefaultSerializationProviderConfiguration service = configBuilder.findServiceByClass(DefaultSerializationProviderConfiguration.class);
if (service == null) {
service = new DefaultSerializationProviderConfiguration();
service.addSerializerFor(clazz, serializer);
return new CacheManagerBuilder<>(this, configBuilder.addService(service));
} else {
DefaultSerializationProviderConfiguration newConfig = new DefaultSerializationProviderConfiguration(service);
newConfig.addSerializerFor(clazz, serializer, true);
return new CacheManagerBuilder<>(this, configBuilder.removeService(service).addService(newConfig));
}
}
/**
* Adds a default {@link SizeOfEngine} configuration, that limits the max object graph to
* size, to the returned builder.
*
* @param size the max object graph size
* @return a new builder with the added configuration
*/
public CacheManagerBuilder<T> withDefaultSizeOfMaxObjectGraph(long size) {
DefaultSizeOfEngineProviderConfiguration configuration = configBuilder.findServiceByClass(DefaultSizeOfEngineProviderConfiguration.class);
if (configuration == null) {
return new CacheManagerBuilder<>(this, configBuilder.addService(new DefaultSizeOfEngineProviderConfiguration(DEFAULT_MAX_OBJECT_SIZE, DEFAULT_UNIT, size)));
} else {
ConfigurationBuilder builder = configBuilder.removeService(configuration);
return new CacheManagerBuilder<>(this, builder.addService(new DefaultSizeOfEngineProviderConfiguration(configuration
.getMaxObjectSize(), configuration.getUnit(), size)));
}
}
/**
* Adds a default {@link SizeOfEngine} configuration, that limits the max object size, to
* the returned builder.
*
* @param size the max object size
* @param unit the max object size unit
* @return a new builder with the added configuration
*/
public CacheManagerBuilder<T> withDefaultSizeOfMaxObjectSize(long size, MemoryUnit unit) {
DefaultSizeOfEngineProviderConfiguration configuration = configBuilder.findServiceByClass(DefaultSizeOfEngineProviderConfiguration.class);
if (configuration == null) {
return new CacheManagerBuilder<>(this, configBuilder.addService(new DefaultSizeOfEngineProviderConfiguration(size, unit, DEFAULT_OBJECT_GRAPH_SIZE)));
} else {
ConfigurationBuilder builder = configBuilder.removeService(configuration);
return new CacheManagerBuilder<>(this, builder.addService(new DefaultSizeOfEngineProviderConfiguration(size, unit, configuration
.getMaxObjectGraphSize())));
}
}
/**
* Adds a {@link WriteBehindProviderConfiguration}, that specifies the thread pool to use, to the returned builder.
*
* @param threadPoolAlias the thread pool alias
* @return a new builder with the added configuration
*
* @see PooledExecutionServiceConfigurationBuilder
*/
public CacheManagerBuilder<T> withDefaultWriteBehindThreadPool(String threadPoolAlias) {
WriteBehindProviderConfiguration config = configBuilder.findServiceByClass(WriteBehindProviderConfiguration.class);
if (config == null) {
return new CacheManagerBuilder<>(this, configBuilder.addService(new WriteBehindProviderConfiguration(threadPoolAlias)));
} else {
ConfigurationBuilder builder = configBuilder.removeService(config);
return new CacheManagerBuilder<>(this, builder.addService(new WriteBehindProviderConfiguration(threadPoolAlias)));
}
}
/**
* Adds a {@link OffHeapDiskStoreProviderConfiguration}, that specifies the thread pool to use, to the returned
* builder.
*
* @param threadPoolAlias the thread pool alias
* @return a new builder with the added configuration
*
* @see PooledExecutionServiceConfigurationBuilder
*/
public CacheManagerBuilder<T> withDefaultDiskStoreThreadPool(String threadPoolAlias) {
OffHeapDiskStoreProviderConfiguration config = configBuilder.findServiceByClass(OffHeapDiskStoreProviderConfiguration.class);
if (config == null) {
return new CacheManagerBuilder<>(this, configBuilder.addService(new OffHeapDiskStoreProviderConfiguration(threadPoolAlias)));
} else {
ConfigurationBuilder builder = configBuilder.removeService(config);
return new CacheManagerBuilder<>(this, builder.addService(new OffHeapDiskStoreProviderConfiguration(threadPoolAlias)));
}
}
/**
* Adds a {@link CacheEventDispatcherFactoryConfiguration}, that specifies the thread pool to use, to the returned
* builder.
*
* @param threadPoolAlias the thread pool alias
* @return a new builder with the added configuration
*
* @see PooledExecutionServiceConfigurationBuilder
*/
public CacheManagerBuilder<T> withDefaultEventListenersThreadPool(String threadPoolAlias) {
CacheEventDispatcherFactoryConfiguration config = configBuilder.findServiceByClass(CacheEventDispatcherFactoryConfiguration.class);
if (config == null) {
return new CacheManagerBuilder<>(this, configBuilder.addService(new CacheEventDispatcherFactoryConfiguration(threadPoolAlias)));
} else {
ConfigurationBuilder builder = configBuilder.removeService(config);
return new CacheManagerBuilder<>(this, builder.addService(new CacheEventDispatcherFactoryConfiguration(threadPoolAlias)));
}
}
/**
* Adds a {@link ServiceCreationConfiguration} to the returned builder.
* <p>
* These configurations are used to load services and configure them at creation time.
*
* @param serviceConfiguration the {@code ServiceCreationConfiguration} to use
* @return a new builder with the added configuration
*/
public CacheManagerBuilder<T> using(ServiceCreationConfiguration<?> serviceConfiguration) {
return new CacheManagerBuilder<>(this, configBuilder.addService(serviceConfiguration));
}
/**
* Replaces an existing {@link ServiceCreationConfiguration} of the same type on the returned builder.
* <p>
* Duplicate service creation configuration will cause a cache manager to fail to initialize.
*
* @param overwriteServiceConfiguration the new {@code ServiceCreationConfiguration} to use
* @return a new builder with the replaced configuration
*/
public CacheManagerBuilder<T> replacing(ServiceCreationConfiguration<?> overwriteServiceConfiguration) {
ServiceCreationConfiguration<?> existingConfiguration = configBuilder.findServiceByClass(overwriteServiceConfiguration.getClass());
return new CacheManagerBuilder<>(this, configBuilder.removeService(existingConfiguration)
.addService(overwriteServiceConfiguration));
}
/**
* Adds a {@link ClassLoader}, to use for non Ehcache types, to the returned builder
*
* @param classLoader the class loader to use
* @return a new builder with the added class loader
*/
public CacheManagerBuilder<T> withClassLoader(ClassLoader classLoader) {
return new CacheManagerBuilder<>(this, configBuilder.withClassLoader(classLoader));
}
/**
* Creates a new {@code CacheManagerBuilder}
*
* @return the cache manager builder
*/
public static CacheManagerBuilder<CacheManager> newCacheManagerBuilder() {
return new CacheManagerBuilder<>();
}
/**
* Convenience method to get a {@link CacheManagerConfiguration} for a {@link PersistentCacheManager} stored on disk. The actual
* level of persistence is configured on the disk resource pool per cache.
*
* @param rootDirectory the root directory to use for disk storage
* @return a {@code CacheManagerConfiguration}
*
* @see ResourcePoolsBuilder#disk(long, MemoryUnit, boolean)
* @see #with(CacheManagerConfiguration)
* @see PersistentCacheManager
*/
public static CacheManagerConfiguration<PersistentCacheManager> persistence(String rootDirectory) {
return persistence(new File(rootDirectory));
}
/**
* Convenience method to get a {@link CacheManagerConfiguration} for a {@link PersistentCacheManager} stored on disk. The actual
* level of persistence is configured on the disk resource pool per cache.
*
* @param rootDirectory the root directory to use for disk storage
* @return a {@code CacheManagerConfiguration}
*
* @see ResourcePoolsBuilder#disk(long, MemoryUnit, boolean)
* @see #with(CacheManagerConfiguration)
* @see PersistentCacheManager
*/
public static CacheManagerConfiguration<PersistentCacheManager> persistence(File rootDirectory) {
return new CacheManagerPersistenceConfiguration(rootDirectory);
}
}
| aurbroszniowski/ehcache3 | impl/src/main/java/org/ehcache/config/builders/CacheManagerBuilder.java | Java | apache-2.0 | 17,637 |
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/
package org.elasticsearch.xpack.core.ml.datafeed;
import org.elasticsearch.common.io.stream.Writeable;
import org.elasticsearch.core.TimeValue;
import org.elasticsearch.common.xcontent.XContentParser;
import org.elasticsearch.test.AbstractSerializingTestCase;
import java.io.IOException;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.nullValue;
import static org.hamcrest.core.Is.is;
public class DelayedDataCheckConfigTests extends AbstractSerializingTestCase<DelayedDataCheckConfig> {
@Override
protected DelayedDataCheckConfig createTestInstance(){
return createRandomizedConfig(100);
}
@Override
protected Writeable.Reader<DelayedDataCheckConfig> instanceReader() {
return DelayedDataCheckConfig::new;
}
@Override
protected DelayedDataCheckConfig doParseInstance(XContentParser parser) {
return DelayedDataCheckConfig.STRICT_PARSER.apply(parser, null);
}
public void testConstructor() {
expectThrows(IllegalArgumentException.class, () -> new DelayedDataCheckConfig(true, TimeValue.MINUS_ONE));
expectThrows(IllegalArgumentException.class, () -> new DelayedDataCheckConfig(true, TimeValue.timeValueHours(25)));
}
public void testEnabledDelayedDataCheckConfig() {
DelayedDataCheckConfig delayedDataCheckConfig = DelayedDataCheckConfig.enabledDelayedDataCheckConfig(TimeValue.timeValueHours(5));
assertThat(delayedDataCheckConfig.isEnabled(), equalTo(true));
assertThat(delayedDataCheckConfig.getCheckWindow(), equalTo(TimeValue.timeValueHours(5)));
}
public void testDisabledDelayedDataCheckConfig() {
DelayedDataCheckConfig delayedDataCheckConfig = DelayedDataCheckConfig.disabledDelayedDataCheckConfig();
assertThat(delayedDataCheckConfig.isEnabled(), equalTo(false));
assertThat(delayedDataCheckConfig.getCheckWindow(), equalTo(null));
}
public void testDefaultDelayedDataCheckConfig() {
DelayedDataCheckConfig delayedDataCheckConfig = DelayedDataCheckConfig.defaultDelayedDataCheckConfig();
assertThat(delayedDataCheckConfig.isEnabled(), equalTo(true));
assertThat(delayedDataCheckConfig.getCheckWindow(), is(nullValue()));
}
public static DelayedDataCheckConfig createRandomizedConfig(long bucketSpanMillis) {
boolean enabled = randomBoolean();
TimeValue timeWindow = null;
if (enabled || randomBoolean()) {
// time span is required to be at least 1 millis, so we use a custom method to generate a time value here
timeWindow = new TimeValue(randomLongBetween(bucketSpanMillis,bucketSpanMillis*2));
}
return new DelayedDataCheckConfig(enabled, timeWindow);
}
@Override
protected DelayedDataCheckConfig mutateInstance(DelayedDataCheckConfig instance) throws IOException {
boolean enabled = instance.isEnabled();
TimeValue timeWindow = instance.getCheckWindow();
switch (between(0, 1)) {
case 0:
enabled = enabled == false;
if (randomBoolean()) {
timeWindow = TimeValue.timeValueMillis(randomLongBetween(1, 1000));
} else {
timeWindow = null;
}
break;
case 1:
if (timeWindow == null) {
timeWindow = TimeValue.timeValueMillis(randomLongBetween(1, 1000));
} else {
timeWindow = new TimeValue(timeWindow.getMillis() + between(10, 100));
}
enabled = true;
break;
default:
throw new AssertionError("Illegal randomisation branch");
}
return new DelayedDataCheckConfig(enabled, timeWindow);
}
}
| ern/elasticsearch | x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/datafeed/DelayedDataCheckConfigTests.java | Java | apache-2.0 | 4,017 |
/**
* Copyright (c) 2014,
* Charles Prud'homme (TASC, INRIA Rennes, LINA CNRS UMR 6241),
* Jean-Guillaume Fages (COSLING S.A.S.).
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of the <organization> nor the
* names of its contributors may be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package org.chocosolver.solver.thread;
/**
* Slave born to be mastered and work in parallel
*
* @author Jean-Guillaume Fages
*/
public abstract class AbstractParallelSlave<P extends AbstractParallelMaster> {
//***********************************************************************************
// VARIABLES
//***********************************************************************************
public P master;
public final int id;
//***********************************************************************************
// CONSTRUCTORS
//***********************************************************************************
/**
* Create a slave born to be mastered and work in parallel
*
* @param master master solver
* @param id slave unique name
*/
public AbstractParallelSlave(P master, int id) {
this.master = master;
this.id = id;
}
//***********************************************************************************
// SUB-PROBLEM SOLVING
//***********************************************************************************
/**
* Creates a new thread to work in parallel
*/
public void workInParallel() {
Thread t = new Thread() {
@Override
public void run() {
work();
master.wishGranted();
}
};
t.start();
}
/**
* do something
*/
public abstract void work();
}
| piyushsh/choco3 | choco-solver/src/main/java/org/chocosolver/solver/thread/AbstractParallelSlave.java | Java | bsd-3-clause | 3,193 |
package org.buildmlearn.toolkit.flashcardtemplate.data;
import org.w3c.dom.Document;
import org.xml.sax.SAXException;
import java.io.File;
import java.io.IOException;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
/**
* Created by Anupam (opticod) on 10/8/16.
*/
/**
* @brief Contains xml data utils for flash card template's simulator.
*/
public class DataUtils {
public static String[] readTitleAuthor() {
String result[] = new String[2];
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
dbf.setValidating(false);
DocumentBuilder db;
Document doc;
try {
File fXmlFile = new File(org.buildmlearn.toolkit.flashcardtemplate.Constants.XMLFileName);
db = dbf.newDocumentBuilder();
doc = db.parse(fXmlFile);
doc.normalize();
result[0] = doc.getElementsByTagName("title").item(0).getChildNodes()
.item(0).getNodeValue();
result[1] = doc.getElementsByTagName("name").item(0).getChildNodes()
.item(0).getNodeValue();
} catch (ParserConfigurationException | SAXException | IOException e) {
e.printStackTrace();
}
return result;
}
}
| opticod/BuildmLearn-Toolkit-Android | source-code/app/src/main/java/org/buildmlearn/toolkit/flashcardtemplate/data/DataUtils.java | Java | bsd-3-clause | 1,360 |
package org.knowm.xchange.bitmarket;
import static org.assertj.core.api.Assertions.assertThat;
import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
import org.knowm.xchange.bitmarket.dto.account.BitMarketBalance;
import org.knowm.xchange.bitmarket.dto.marketdata.BitMarketOrderBook;
import org.knowm.xchange.bitmarket.dto.marketdata.BitMarketTicker;
import org.knowm.xchange.bitmarket.dto.marketdata.BitMarketTrade;
import org.knowm.xchange.bitmarket.dto.trade.BitMarketOrder;
import org.knowm.xchange.dto.account.Balance;
import org.knowm.xchange.dto.marketdata.OrderBook;
import org.knowm.xchange.dto.marketdata.Ticker;
import org.knowm.xchange.dto.marketdata.Trade;
import org.knowm.xchange.dto.trade.LimitOrder;
import org.knowm.xchange.dto.trade.UserTrade;
public class BitMarketAssert {
public static void assertEquals(Balance o1, Balance o2) {
assertThat(o1.getCurrency()).isEqualTo(o2.getCurrency());
assertThat(o1.getTotal()).isEqualTo(o2.getTotal());
assertThat(o1.getAvailable()).isEqualTo(o2.getAvailable());
assertThat(o1.getFrozen()).isEqualTo(o2.getFrozen());
}
public static void assertEquals(Trade o1, Trade o2) {
assertThat(o1.getType()).isEqualTo(o2.getType());
assertThat(o1.getOriginalAmount()).isEqualTo(o2.getOriginalAmount());
assertThat(o1.getCurrencyPair()).isEqualTo(o2.getCurrencyPair());
assertThat(o1.getPrice()).isEqualTo(o2.getPrice());
assertThat(o1.getTimestamp()).isEqualTo(o2.getTimestamp());
assertThat(o1.getId()).isEqualTo(o2.getId());
}
public static void assertEquals(UserTrade o1, UserTrade o2) {
assertThat(o1.getType()).isEqualTo(o2.getType());
assertThat(o1.getOriginalAmount()).isEqualTo(o2.getOriginalAmount());
assertThat(o1.getCurrencyPair()).isEqualTo(o2.getCurrencyPair());
assertThat(o1.getPrice()).isEqualTo(o2.getPrice());
assertThat(o1.getTimestamp()).isEqualTo(o2.getTimestamp());
assertThat(o1.getId()).isEqualTo(o2.getId());
assertThat(o1.getOrderId()).isEqualTo(o2.getOrderId());
assertThat(o1.getFeeAmount()).isEqualTo(o2.getFeeAmount());
assertThat(o1.getFeeCurrency()).isEqualTo(o2.getFeeCurrency());
}
public static void assertEquals(LimitOrder o1, LimitOrder o2) {
assertThat(o1.getId()).isEqualTo(o2.getId());
assertThat(o1.getType()).isEqualTo(o2.getType());
assertThat(o1.getCurrencyPair()).isEqualTo(o2.getCurrencyPair());
assertThat(o1.getLimitPrice()).isEqualTo(o2.getLimitPrice());
assertThat(o1.getOriginalAmount()).isEqualTo(o2.getOriginalAmount());
assertThat(o1.getTimestamp()).isEqualTo(o2.getTimestamp());
}
public static void assertEqualsWithoutTimestamp(LimitOrder o1, LimitOrder o2) {
assertThat(o1.getId()).isEqualTo(o2.getId());
assertThat(o1.getType()).isEqualTo(o2.getType());
assertThat(o1.getCurrencyPair()).isEqualTo(o2.getCurrencyPair());
assertThat(o1.getLimitPrice()).isEqualTo(o2.getLimitPrice());
assertThat(o1.getOriginalAmount()).isEqualTo(o2.getOriginalAmount());
}
public static void assertEquals(Ticker o1, Ticker o2) {
assertThat(o1.getBid()).isEqualTo(o2.getBid());
assertThat(o1.getAsk()).isEqualTo(o2.getAsk());
assertThat(o1.getCurrencyPair()).isEqualTo(o2.getCurrencyPair());
assertThat(o1.getHigh()).isEqualTo(o2.getHigh());
assertThat(o1.getLast()).isEqualTo(o2.getLast());
assertThat(o1.getLow()).isEqualTo(o2.getLow());
assertThat(o1.getTimestamp()).isEqualTo(o2.getTimestamp());
assertThat(o1.getVolume()).isEqualTo(o2.getVolume());
assertThat(o1.getVwap()).isEqualTo(o2.getVwap());
}
public static void assertEquals(OrderBook o1, OrderBook o2) {
assertThat(o1.getTimeStamp()).isEqualTo(o2.getTimeStamp());
assertEquals(o1.getAsks(), o2.getAsks());
assertEquals(o1.getBids(), o2.getBids());
}
public static void assertEquals(List<LimitOrder> o1, List<LimitOrder> o2) {
assertThat(o1.size()).isEqualTo(o2.size());
for (int i = 0; i < o1.size(); i++) {
assertEqualsWithoutTimestamp(o1.get(i), o2.get(i));
}
}
public static void assertEquals(BitMarketOrder o1, BitMarketOrder o2) {
assertThat(o1.getId()).isEqualTo(o2.getId());
assertThat(o1.getMarket()).isEqualTo(o2.getMarket());
assertThat(o1.getAmount()).isEqualTo(o2.getAmount());
assertThat(o1.getRate()).isEqualTo(o2.getRate());
assertThat(o1.getFiat()).isEqualTo(o2.getFiat());
assertThat(o1.getType()).isEqualTo(o2.getType());
assertThat(o1.getTime()).isEqualTo(o2.getTime());
}
public static void assertEquals(BitMarketOrderBook o1, BitMarketOrderBook o2) {
assertEquals(o1.getAsks(), o2.getAsks());
assertEquals(o1.getBids(), o2.getBids());
assertThat(o1.toString()).isEqualTo(o2.toString());
}
public static void assertEquals(BitMarketTicker o1, BitMarketTicker o2) {
assertThat(o1.getAsk()).isEqualTo(o2.getAsk());
assertThat(o1.getBid()).isEqualTo(o2.getBid());
assertThat(o1.getLast()).isEqualTo(o2.getLast());
assertThat(o1.getLow()).isEqualTo(o2.getLow());
assertThat(o1.getHigh()).isEqualTo(o2.getHigh());
assertThat(o1.getVwap()).isEqualTo(o2.getVwap());
assertThat(o1.getVolume()).isEqualTo(o2.getVolume());
assertThat(o1.toString()).isEqualTo(o2.toString());
}
public static void assertEquals(BitMarketTrade o1, BitMarketTrade o2) {
assertThat(o1.getTid()).isEqualTo(o2.getTid());
assertThat(o1.getPrice()).isEqualTo(o2.getPrice());
assertThat(o1.getAmount()).isEqualTo(o2.getAmount());
assertThat(o1.getDate()).isEqualTo(o2.getDate());
assertThat(o1.toString()).isEqualTo(o2.toString());
}
public static void assertEquals(BitMarketBalance o1, BitMarketBalance o2) {
assertEquals(o1.getAvailable(), o2.getAvailable());
assertEquals(o1.getBlocked(), o2.getBlocked());
}
private static void assertEquals(Map<String, BigDecimal> o1, Map<String, BigDecimal> o2) {
assertThat(o1.size()).isEqualTo(o2.size());
for (String key : o1.keySet()) {
assertThat(o1.get(key)).isEqualTo(o2.get(key));
}
}
private static void assertEquals(BigDecimal[][] o1, BigDecimal[][] o2) {
assertThat(o1.length).isEqualTo(o2.length);
for (int i = 0; i < o1.length; i++) {
assertThat(o1[i].length).isEqualTo(o2[i].length);
for (int j = 0; j < o1[i].length; j++) {
assertThat(o1[i][j]).isEqualTo(o2[i][j]);
}
}
}
}
| chrisrico/XChange | xchange-bitmarket/src/test/java/org/knowm/xchange/bitmarket/BitMarketAssert.java | Java | mit | 6,414 |
package org.knowm.xchange.test.exx;
import java.io.IOException;
import org.knowm.xchange.Exchange;
import org.knowm.xchange.ExchangeFactory;
import org.knowm.xchange.ExchangeSpecification;
import org.knowm.xchange.exx.EXXExchange;
import org.knowm.xchange.service.account.AccountService;
/**
* kevinobamatheus@gmail.com
*
* @author kevingates
*/
public class AccountServiceIntegration {
public static void main(String[] args) {
try {
getAssetInfo();
} catch (IOException e) {
e.printStackTrace();
}
}
private static void getAssetInfo() throws IOException {
String apiKey = "";
String secretKey = "";
Exchange exchange = ExchangeFactory.INSTANCE.createExchange(EXXExchange.class.getName());
ExchangeSpecification exchangeSpecification = exchange.getDefaultExchangeSpecification();
exchangeSpecification.setSslUri("https://trade.exx.com");
exchangeSpecification.setApiKey(apiKey);
exchangeSpecification.setSecretKey(secretKey);
exchange.applySpecification(exchangeSpecification);
AccountService accountService = exchange.getAccountService();
try {
System.out.println("accountInfo");
System.out.println(accountService.getAccountInfo());
System.out.println(accountService.getAccountInfo().getWallets());
} catch (IOException e) {
e.printStackTrace();
}
}
}
| chrisrico/XChange | xchange-exx/src/test/java/org/knowm/xchange/test/exx/AccountServiceIntegration.java | Java | mit | 1,369 |
package gueei.binding;
import java.util.Collection;
import java.util.ArrayList;
public abstract class DependentObservable<T> extends Observable<T> implements Observer{
protected IObservable<?>[] mDependents;
public DependentObservable(Class<T> type, IObservable<?>... dependents) {
super(type);
for(IObservable<?> o : dependents){
o.subscribe(this);
}
this.mDependents = dependents;
this.onPropertyChanged(null, new ArrayList<Object>());
}
// This is provided in case the constructor can't be used.
// Not intended for normal usage
public void addDependents(IObservable<?>... dependents){
IObservable<?>[] temp = mDependents;
mDependents = new IObservable<?>[temp.length + dependents.length];
int len = temp.length;
for(int i=0; i<len; i++){
mDependents[i] = temp[i];
}
int len2 = dependents.length;
for(int i=0; i<len2; i++){
mDependents[i+len] = dependents[i];
dependents[i].subscribe(this);
}
this.onPropertyChanged(null, new ArrayList<Object>());
}
public abstract T calculateValue(Object... args) throws Exception;
public final void onPropertyChanged(IObservable<?> prop,
Collection<Object> initiators) {
dirty = true;
initiators.add(this);
this.notifyChanged(initiators);
}
private boolean dirty = false;
@Override
public T get() {
if (dirty){
int len = mDependents.length;
Object[] values = new Object[len];
for(int i=0; i<len; i++){
values[i] = mDependents[i].get();
}
try{
T value = this.calculateValue(values);
this.setWithoutNotify(value);
}catch(Exception e){
BindingLog.exception
("DependentObservable.CalculateValue()", e);
}
dirty = false;
}
return super.get();
}
public boolean isDirty() {
return dirty;
}
public void setDirty(boolean dirty) {
this.dirty = dirty;
}
} | yangqiang1223/AndroidBinding | Core/AndroidBinding/src/gueei/binding/DependentObservable.java | Java | mit | 1,894 |
package com.iluwatar;
public class Sergeant extends Unit {
public Sergeant(Unit ... children) {
super(children);
}
@Override
public void accept(UnitVisitor visitor) {
visitor.visitSergeant(this);
super.accept(visitor);
}
@Override
public String toString() {
return "sergeant";
}
}
| zfu/java-design-patterns | visitor/src/main/java/com/iluwatar/Sergeant.java | Java | mit | 321 |
/*******************************************************************************
* Copyright (c) 2012-2015 Codenvy, S.A.
* 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:
* Codenvy, S.A. - initial API and implementation
*******************************************************************************/
package org.eclipse.che.ide.actions;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.inject.Inject;
import com.google.inject.Singleton;
import org.eclipse.che.api.analytics.client.logger.AnalyticsEventLogger;
import org.eclipse.che.ide.Resources;
import org.eclipse.che.ide.api.action.ActionEvent;
import org.eclipse.che.ide.api.action.ProjectAction;
import org.eclipse.che.ide.api.editor.EditorAgent;
import org.eclipse.che.ide.api.editor.EditorInput;
import org.eclipse.che.ide.api.editor.EditorPartPresenter;
import org.eclipse.che.ide.api.editor.EditorWithAutoSave;
import org.eclipse.che.ide.util.loging.Log;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
/** @author Evgen Vidolob */
@Singleton
public class SaveAllAction extends ProjectAction {
private final EditorAgent editorAgent;
private final AnalyticsEventLogger eventLogger;
@Inject
public SaveAllAction(EditorAgent editorAgent, Resources resources, AnalyticsEventLogger eventLogger) {
super("Save All", "Save all changes for project", resources.save());
this.editorAgent = editorAgent;
this.eventLogger = eventLogger;
}
/** {@inheritDoc} */
@Override
public void actionPerformed(ActionEvent e) {
eventLogger.log(this);
Collection<EditorPartPresenter> values = editorAgent.getOpenedEditors().values();
List<EditorPartPresenter> editors = new ArrayList<>(values);
save(editors);
}
private void save(final List<EditorPartPresenter> editors) {
if (editors.isEmpty()) {
return;
}
final EditorPartPresenter editorPartPresenter = editors.get(0);
if (editorPartPresenter.isDirty()) {
editorPartPresenter.doSave(new AsyncCallback<EditorInput>() {
@Override
public void onFailure(Throwable caught) {
Log.error(SaveAllAction.class, caught);
//try to save other files
editors.remove(editorPartPresenter);
save(editors);
}
@Override
public void onSuccess(EditorInput result) {
editors.remove(editorPartPresenter);
save(editors);
}
});
} else {
editors.remove(editorPartPresenter);
save(editors);
}
}
/** {@inheritDoc} */
@Override
public void updateProjectAction(ActionEvent e) {
// e.getPresentation().setVisible(true);
boolean hasDirtyEditor = false;
for (EditorPartPresenter editor : editorAgent.getOpenedEditors().values()) {
if(editor instanceof EditorWithAutoSave) {
if (((EditorWithAutoSave)editor).isAutoSaveEnabled()) {
continue;
}
}
if (editor.isDirty()) {
hasDirtyEditor = true;
break;
}
}
e.getPresentation().setEnabledAndVisible(hasDirtyEditor);
}
}
| Ori-Libhaber/che-core | ide/che-core-ide-app/src/main/java/org/eclipse/che/ide/actions/SaveAllAction.java | Java | epl-1.0 | 3,624 |
/*
* Created on May 13, 2003
*========================================================================
* Modifications history
*========================================================================
* $Log: PredicateWordRule.java,v $
* Revision 1.2 2003/05/30 20:53:09 agfitzp
* 0.0.2 : Outlining is now done as the user types. Some other bug fixes.
*
*========================================================================
*/
package net.sourceforge.jseditor.editors;
import org.eclipse.jface.text.rules.ICharacterScanner;
import org.eclipse.jface.text.rules.IPredicateRule;
import org.eclipse.jface.text.rules.IToken;
import org.eclipse.jface.text.rules.Token;
import org.eclipse.jface.text.rules.WordRule;
import org.eclipse.jface.text.rules.IWordDetector;
/**
* @author fitzpata
*/
public class PredicateWordRule extends WordRule implements IPredicateRule {
/* (non-Javadoc)
* @see org.eclipse.jface.text.rules.IPredicateRule#getSuccessToken()
*/
protected IToken successToken = Token.UNDEFINED;
public void addWords(String[] tokens, IToken token)
{
for (int i = 0; i < tokens.length; i++) {
addWord(tokens[i], token);
}
}
public IToken getSuccessToken() {
return successToken;
}
/* (non-Javadoc)
* @see org.eclipse.jface.text.rules.IPredicateRule#evaluate(org.eclipse.jface.text.rules.ICharacterScanner, boolean)
*/
public IToken evaluate(ICharacterScanner scanner, boolean resume) {
successToken = this.evaluate(scanner, resume);//true);
return successToken;
}
/**
* Creates a rule which, with the help of an word detector, will return the token
* associated with the detected word. If no token has been associated, the scanner
* will be rolled back and an undefined token will be returned in order to allow
* any subsequent rules to analyze the characters.
*
* @param detector the word detector to be used by this rule, may not be <code>null</code>
*
* @see #addWord
*/
public PredicateWordRule(IWordDetector detector) {
super(detector);
}
/**
* Creates a rule which, with the help of an word detector, will return the token
* associated with the detected word. If no token has been associated, the
* specified default token will be returned.
*
* @param detector the word detector to be used by this rule, may not be <code>null</code>
* @param defaultToken the default token to be returned on success
* if nothing else is specified, may not be <code>null</code>
*
* @see #addWord
*/
public PredicateWordRule(IWordDetector detector, IToken defaultToken) {
super(detector, defaultToken);
}
public PredicateWordRule(IWordDetector detector, String tokenString, IToken tokenType) {
super(detector);
this.addWord(tokenString, tokenType);
}
public PredicateWordRule(IWordDetector detector, String[] tokens, IToken tokenType) {
super(detector);
this.addWords(tokens, tokenType);
}
public PredicateWordRule(IWordDetector detector, IToken defaultToken, String[] tokens, IToken tokenType) {
super(detector, defaultToken);
this.addWords(tokens, tokenType);
}
}
| royleexhFake/mayloon-portingtool | net.sourceforge.jseditor/src-jseditor/net/sourceforge/jseditor/editors/PredicateWordRule.java | Java | epl-1.0 | 3,101 |
/*******************************************************************************
* Copyright (c) 2012-2015 Codenvy, S.A.
* 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:
* Codenvy, S.A. - initial API and implementation
*******************************************************************************/
package org.eclipse.che.api.core.util;
import org.testng.Assert;
import org.testng.annotations.Test;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
/** @author andrew00x */
public class ProcessUtilTest {
@Test
public void testKill() throws Exception {
final Process p = Runtime.getRuntime().exec(new String[]{"/bin/bash", "-c", "sleep 10; echo wake\\ up"});
final List<String> stdout = new ArrayList<>();
final List<String> stderr = new ArrayList<>();
final IOException[] processError = new IOException[1];
final CountDownLatch latch = new CountDownLatch(1);
final long start = System.currentTimeMillis();
new Thread() {
public void run() {
try {
ProcessUtil.process(p,
new LineConsumer() {
@Override
public void writeLine(String line) throws IOException {
stdout.add(line);
}
@Override
public void close() throws IOException {
}
},
new LineConsumer() {
@Override
public void writeLine(String line) throws IOException {
stderr.add(line);
}
@Override
public void close() throws IOException {
}
}
);
} catch (IOException e) {
processError[0] = e; // throw when kill process
} finally {
latch.countDown();
}
}
}.start();
Thread.sleep(1000); // give time to start process
Assert.assertTrue(ProcessUtil.isAlive(p), "Process is not started.");
ProcessUtil.kill(p); // kill process
latch.await(15, TimeUnit.SECONDS); // should not stop here if process killed
final long end = System.currentTimeMillis();
// System process sleeps 10 seconds. It is safety to check we done in less then 3 sec.
Assert.assertTrue((end - start) < 3000, "Fail kill process");
System.out.println(processError[0]);
//processError[0].printStackTrace();
System.out.println(stdout);
System.out.println(stderr);
}
}
| aljiru/che-core | platform-api/che-core-api-core/src/test/java/org/eclipse/che/api/core/util/ProcessUtilTest.java | Java | epl-1.0 | 3,462 |
/**
* Copyright (c) 2005-2012 by Appcelerator, Inc. All Rights Reserved.
* Licensed under the terms of the Eclipse Public License (EPL).
* Please see the license.txt included with this distribution for details.
* Any modifications to this file must keep this entire header intact.
*/
package com.python.pydev.refactoring.wizards.rename.visitors;
import java.util.Stack;
import org.python.pydev.parser.jython.SimpleNode;
import org.python.pydev.parser.jython.Visitor;
import org.python.pydev.parser.jython.ast.Call;
import org.python.pydev.parser.jython.ast.Name;
import org.python.pydev.parser.jython.ast.NameTok;
/**
* This visitor is used to find a call given its ast
*
* @author Fabio
*/
public class FindCallVisitor extends Visitor {
private Name name;
private NameTok nameTok;
private Call call;
private Stack<Call> lastCall = new Stack<Call>();
public FindCallVisitor(Name name) {
this.name = name;
}
public FindCallVisitor(NameTok nameTok) {
this.nameTok = nameTok;
}
public Call getCall() {
return call;
}
@Override
public Object visitCall(Call node) throws Exception {
if (this.call != null) {
return null;
}
if (node.func == name) {
//check the name (direct)
this.call = node;
} else if (nameTok != null) {
//check the name tok (inside of attribute)
lastCall.push(node);
Object r = super.visitCall(node);
lastCall.pop();
if (this.call != null) {
return null;
}
return r;
}
if (this.call != null) {
return null;
}
return super.visitCall(node);
}
@Override
public Object visitNameTok(NameTok node) throws Exception {
if (node == nameTok) {
if (lastCall.size() > 0) {
call = lastCall.peek();
}
return null;
}
return super.visitNameTok(node);
}
public static Call findCall(NameTok nametok, SimpleNode root) {
FindCallVisitor visitor = new FindCallVisitor(nametok);
try {
visitor.traverse(root);
} catch (Exception e) {
throw new RuntimeException(e);
}
return visitor.call;
}
public static Call findCall(Name name, SimpleNode root) {
FindCallVisitor visitor = new FindCallVisitor(name);
try {
visitor.traverse(root);
} catch (Exception e) {
throw new RuntimeException(e);
}
return visitor.call;
}
}
| rgom/Pydev | plugins/com.python.pydev.refactoring/src/com/python/pydev/refactoring/wizards/rename/visitors/FindCallVisitor.java | Java | epl-1.0 | 2,648 |
package com.intel.ide.eclipse.mpt.classpath;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Properties;
import com.intel.ide.eclipse.mpt.launching.J2SCyclicProjectUtils;
public class ContactedClasses extends Resource implements IExternalResource, IClasspathContainer {
private String binRelativePath;
private List classList;
private List externalList;
public void load() {
File file = getAbsoluteFile();
classList = new ArrayList();
externalList = new ArrayList();
if (file.exists()) {
Properties props = PathUtil.loadJZ(file);
String[] reses = PathUtil.getResources(props);
binRelativePath = props.getProperty(PathUtil.J2S_OUTPUT_PATH);
for (int i = 0; i < reses.length; i++) {
if (reses[i] != null) {
String res = reses[i].trim();
if (res.endsWith(".z.js")) {
ContactedClasses jz = new ContactedClasses();
jz.setFolder(this.getAbsoluteFolder());
jz.setRelativePath(res);
jz.setParent(this);
externalList.add(jz);
} else if (res.endsWith(".js")) {
ContactedUnitClass unit = new ContactedUnitClass();
unit.setFolder(this.getAbsoluteFolder());
unit.setRelativePath(res);
unit.parseClassName();
unit.setParent(this);
classList.add(unit);
} else if (res.endsWith(".css")) {
CSSResource css = new CSSResource();
css.setFolder(this.getAbsoluteFolder());
css.setRelativePath(res);
css.setParent(this);
externalList.add(css);
}
}
}
}
}
public void store(Properties props) {
Resource[] reses = getChildren();
StringBuffer buf = new StringBuffer();
for (int i = 0; i < reses.length; i++) {
String str = reses[i].toResourceString();
buf.append(str);
if (i != reses.length - 1) {
buf.append(",");
}
}
props.setProperty(PathUtil.J2S_RESOURCES_LIST, buf.toString());
props.setProperty(PathUtil.J2S_OUTPUT_PATH, binRelativePath);
}
public Resource[] getChildren() {
if (externalList == null || classList == null) {
this.load();
}
int size = externalList.size();
Resource[] res = new Resource[classList.size() + size];
for (int i = 0; i < size; i++) {
res[i] = (Resource) externalList.get(i);
}
for (int i = 0; i < classList.size(); i++) {
res[i + size] = (Resource) classList.get(i);
}
return res;
}
public ContactedUnitClass[] getClasses() {
return (ContactedUnitClass[]) classList.toArray(new ContactedClasses[0]);
}
public IExternalResource[] getExternals() {
return (IExternalResource[]) externalList.toArray(new IExternalResource[0]);
}
public String getBinRelativePath() {
return binRelativePath;
}
public void setBinRelativePath(String binRelativePath) {
this.binRelativePath = binRelativePath;
}
public String toHTMLString() {
if (getRelativePath() != null && getRelativePath().endsWith(".j2x")) {
return "";
}
Resource p = this.getParent();
if (p != null) {
if (p instanceof ContactedClasses) {
Resource pp = p.getParent();
if (pp != null && pp instanceof ContactedClasses) {
return "";
}
}
}
StringBuffer buf = new StringBuffer();
if (externalList == null) {
this.load();
}
for (Iterator iter = externalList.iterator(); iter.hasNext();) {
Resource res = (Resource) iter.next();
if (!J2SCyclicProjectUtils.visit(res)) {
continue;
}
buf.append(res.toHTMLString());
}
buf.append("<script type=\"text/javascript\" src=\"");
String binFolder = getBinRelativePath();
if (binFolder != null) {
String binPath = binFolder.trim();
if (binPath.length() != 0) {
buf.append(binPath);
if (!binPath.endsWith("/")) {
buf.append("/");
}
}
}
if (p != null) {
if (p instanceof ContactedClasses) {
ContactedClasses cc = (ContactedClasses) p;
String path = cc.getRelativePath();
int idx = path.lastIndexOf('/');
if (idx != -1) {
buf.append(path.substring(0, idx + 1));
}
} else if (p instanceof CompositeResources) {
CompositeResources cc = (CompositeResources) p;
String binRelative = cc.getBinRelativePath();
if (binRelative != null) {
if (binRelative.length() != 0 && getRelativePath().endsWith(".z.js")) {
return "";
}
buf.append(binRelative);
}
}
}
buf.append(getRelativePath());
buf.append("\"></script>\r\n");
return buf.toString();
}
public String toJ2XString() {
if (getName().endsWith(".j2x")) {
try {
return getAbsoluteFile().getCanonicalPath();
} catch (IOException e) {
e.printStackTrace();
}
}
return "";
}
public int getType() {
return CONTAINER;
}
}
| royleexhFake/mayloon-portingtool | com.intel.ide.eclipse.mpt/src/com/intel/ide/eclipse/mpt/classpath/ContactedClasses.java | Java | epl-1.0 | 4,878 |
/*
* Copyright (C) 2014 The Android Open Source Project
* Copyright (c) 2000, 2008, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code 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 General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package java.nio;
/**
* A read/write HeapLongBuffer.
*/
class HeapLongBuffer
extends LongBuffer {
// For speed these fields are actually declared in X-Buffer;
// these declarations are here as documentation
/*
protected final long[] hb;
protected final int offset;
*/
HeapLongBuffer(int cap, int lim) { // package-private
this(cap, lim, false);
}
HeapLongBuffer(int cap, int lim, boolean isReadOnly) { // package-private
super(-1, 0, lim, cap, new long[cap], 0);
this.isReadOnly = isReadOnly;
}
HeapLongBuffer(long[] buf, int off, int len) { // package-private
this(buf, off, len, false);
}
HeapLongBuffer(long[] buf, int off, int len, boolean isReadOnly) { // package-private
super(-1, off, off + len, buf.length, buf, 0);
this.isReadOnly = isReadOnly;
}
protected HeapLongBuffer(long[] buf,
int mark, int pos, int lim, int cap,
int off) {
this(buf, mark, pos, lim, cap, off, false);
}
protected HeapLongBuffer(long[] buf,
int mark, int pos, int lim, int cap,
int off, boolean isReadOnly) {
super(mark, pos, lim, cap, buf, off);
this.isReadOnly = isReadOnly;
}
public LongBuffer slice() {
return new HeapLongBuffer(hb,
-1,
0,
this.remaining(),
this.remaining(),
this.position() + offset,
isReadOnly);
}
public LongBuffer duplicate() {
return new HeapLongBuffer(hb,
this.markValue(),
this.position(),
this.limit(),
this.capacity(),
offset,
isReadOnly);
}
public LongBuffer asReadOnlyBuffer() {
return new HeapLongBuffer(hb,
this.markValue(),
this.position(),
this.limit(),
this.capacity(),
offset, true);
}
protected int ix(int i) {
return i + offset;
}
public long get() {
return hb[ix(nextGetIndex())];
}
public long get(int i) {
return hb[ix(checkIndex(i))];
}
public LongBuffer get(long[] dst, int offset, int length) {
checkBounds(offset, length, dst.length);
if (length > remaining())
throw new BufferUnderflowException();
System.arraycopy(hb, ix(position()), dst, offset, length);
position(position() + length);
return this;
}
public boolean isDirect() {
return false;
}
public boolean isReadOnly() {
return isReadOnly;
}
public LongBuffer put(long x) {
if (isReadOnly) {
throw new ReadOnlyBufferException();
}
hb[ix(nextPutIndex())] = x;
return this;
}
public LongBuffer put(int i, long x) {
if (isReadOnly) {
throw new ReadOnlyBufferException();
}
hb[ix(checkIndex(i))] = x;
return this;
}
public LongBuffer put(long[] src, int offset, int length) {
if (isReadOnly) {
throw new ReadOnlyBufferException();
}
checkBounds(offset, length, src.length);
if (length > remaining())
throw new BufferOverflowException();
System.arraycopy(src, offset, hb, ix(position()), length);
position(position() + length);
return this;
}
public LongBuffer put(LongBuffer src) {
if (isReadOnly) {
throw new ReadOnlyBufferException();
}
if (src instanceof HeapLongBuffer) {
if (src == this)
throw new IllegalArgumentException();
HeapLongBuffer sb = (HeapLongBuffer) src;
int n = sb.remaining();
if (n > remaining())
throw new BufferOverflowException();
System.arraycopy(sb.hb, sb.ix(sb.position()),
hb, ix(position()), n);
sb.position(sb.position() + n);
position(position() + n);
} else if (src.isDirect()) {
int n = src.remaining();
if (n > remaining())
throw new BufferOverflowException();
src.get(hb, ix(position()), n);
position(position() + n);
} else {
super.put(src);
}
return this;
}
public LongBuffer compact() {
if (isReadOnly) {
throw new ReadOnlyBufferException();
}
System.arraycopy(hb, ix(position()), hb, ix(0), remaining());
position(remaining());
limit(capacity());
discardMark();
return this;
}
public ByteOrder order() {
return ByteOrder.nativeOrder();
}
}
| AdmireTheDistance/android_libcore | ojluni/src/main/java/java/nio/HeapLongBuffer.java | Java | gpl-2.0 | 6,185 |
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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 opennlp.tools.doccat;
import java.util.Collection;
import java.util.LinkedList;
import java.util.Map;
/**
* Context generator for document categorizer
*/
class DocumentCategorizerContextGenerator {
private FeatureGenerator[] mFeatureGenerators;
DocumentCategorizerContextGenerator(FeatureGenerator... featureGenerators) {
mFeatureGenerators = featureGenerators;
}
public String[] getContext(String[] text, Map<String, Object> extraInformation) {
Collection<String> context = new LinkedList<>();
for (FeatureGenerator mFeatureGenerator : mFeatureGenerators) {
Collection<String> extractedFeatures =
mFeatureGenerator.extractFeatures(text, extraInformation);
context.addAll(extractedFeatures);
}
return context.toArray(new String[context.size()]);
}
}
| manjeetk09/GoogleScrapper | opennlp/tools/doccat/DocumentCategorizerContextGenerator.java | Java | gpl-2.0 | 1,622 |
/*
* Copyright (c) 1997, 2018, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code 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 General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package javax.crypto;
import java.io.*;
/**
* A CipherOutputStream is composed of an OutputStream and a Cipher so
* that write() methods first process the data before writing them out
* to the underlying OutputStream. The cipher must be fully
* initialized before being used by a CipherOutputStream.
*
* <p> For example, if the cipher is initialized for encryption, the
* CipherOutputStream will attempt to encrypt data before writing out the
* encrypted data.
*
* <p> This class adheres strictly to the semantics, especially the
* failure semantics, of its ancestor classes
* java.io.OutputStream and java.io.FilterOutputStream. This class
* has exactly those methods specified in its ancestor classes, and
* overrides them all. Moreover, this class catches all exceptions
* that are not thrown by its ancestor classes. In particular, this
* class catches BadPaddingException and other exceptions thrown by
* failed integrity checks during decryption. These exceptions are not
* re-thrown, so the client will not be informed that integrity checks
* failed. Because of this behavior, this class may not be suitable
* for use with decryption in an authenticated mode of operation (e.g. GCM)
* if the application requires explicit notification when authentication
* fails. Such an application can use the Cipher API directly as an
* alternative to using this class.
*
* <p> It is crucial for a programmer using this class not to use
* methods that are not defined or overriden in this class (such as a
* new method or constructor that is later added to one of the super
* classes), because the design and implementation of those methods
* are unlikely to have considered security impact with regard to
* CipherOutputStream.
*
* @author Li Gong
* @see java.io.OutputStream
* @see java.io.FilterOutputStream
* @see javax.crypto.Cipher
* @see javax.crypto.CipherInputStream
*
* @since 1.4
*/
public class CipherOutputStream extends FilterOutputStream {
// the cipher engine to use to process stream data
private Cipher cipher;
// the underlying output stream
private OutputStream output;
/* the buffer holding one byte of incoming data */
private byte[] ibuffer = new byte[1];
// the buffer holding data ready to be written out
private byte[] obuffer;
// stream status
private boolean closed = false;
/**
*
* Constructs a CipherOutputStream from an OutputStream and a
* Cipher.
* <br>Note: if the specified output stream or cipher is
* null, a NullPointerException may be thrown later when
* they are used.
*
* @param os the OutputStream object
* @param c an initialized Cipher object
*/
public CipherOutputStream(OutputStream os, Cipher c) {
super(os);
output = os;
cipher = c;
};
/**
* Constructs a CipherOutputStream from an OutputStream without
* specifying a Cipher. This has the effect of constructing a
* CipherOutputStream using a NullCipher.
* <br>Note: if the specified output stream is null, a
* NullPointerException may be thrown later when it is used.
*
* @param os the OutputStream object
*/
protected CipherOutputStream(OutputStream os) {
super(os);
output = os;
cipher = new NullCipher();
}
/**
* Writes the specified byte to this output stream.
*
* @param b the <code>byte</code>.
* @exception IOException if an I/O error occurs.
* @since JCE1.2
*/
public void write(int b) throws IOException {
ibuffer[0] = (byte) b;
obuffer = cipher.update(ibuffer, 0, 1);
if (obuffer != null) {
output.write(obuffer);
obuffer = null;
}
};
/**
* Writes <code>b.length</code> bytes from the specified byte array
* to this output stream.
* <p>
* The <code>write</code> method of
* <code>CipherOutputStream</code> calls the <code>write</code>
* method of three arguments with the three arguments
* <code>b</code>, <code>0</code>, and <code>b.length</code>.
*
* @param b the data.
* @exception NullPointerException if <code>b</code> is null.
* @exception IOException if an I/O error occurs.
* @see javax.crypto.CipherOutputStream#write(byte[], int, int)
* @since JCE1.2
*/
public void write(byte b[]) throws IOException {
write(b, 0, b.length);
}
/**
* Writes <code>len</code> bytes from the specified byte array
* starting at offset <code>off</code> to this output stream.
*
* @param b the data.
* @param off the start offset in the data.
* @param len the number of bytes to write.
* @exception IOException if an I/O error occurs.
* @since JCE1.2
*/
public void write(byte b[], int off, int len) throws IOException {
obuffer = cipher.update(b, off, len);
if (obuffer != null) {
output.write(obuffer);
obuffer = null;
}
}
/**
* Flushes this output stream by forcing any buffered output bytes
* that have already been processed by the encapsulated cipher object
* to be written out.
*
* <p>Any bytes buffered by the encapsulated cipher
* and waiting to be processed by it will not be written out. For example,
* if the encapsulated cipher is a block cipher, and the total number of
* bytes written using one of the <code>write</code> methods is less than
* the cipher's block size, no bytes will be written out.
*
* @exception IOException if an I/O error occurs.
* @since JCE1.2
*/
public void flush() throws IOException {
if (obuffer != null) {
output.write(obuffer);
obuffer = null;
}
output.flush();
}
/**
* Closes this output stream and releases any system resources
* associated with this stream.
* <p>
* This method invokes the <code>doFinal</code> method of the encapsulated
* cipher object, which causes any bytes buffered by the encapsulated
* cipher to be processed. The result is written out by calling the
* <code>flush</code> method of this output stream.
* <p>
* This method resets the encapsulated cipher object to its initial state
* and calls the <code>close</code> method of the underlying output
* stream.
*
* @exception IOException if an I/O error occurs.
* @since JCE1.2
*/
public void close() throws IOException {
if (closed) {
return;
}
closed = true;
try {
obuffer = cipher.doFinal();
} catch (IllegalBlockSizeException | BadPaddingException e) {
obuffer = null;
}
try {
flush();
} catch (IOException ignored) {}
out.close();
}
}
| JetBrains/jdk8u_jdk | src/share/classes/javax/crypto/CipherOutputStream.java | Java | gpl-2.0 | 8,241 |
/*
* Copyright (c) 1998-2012 Caucho Technology -- all rights reserved
*
* This file is part of Resin(R) Open Source
*
* Each copy or derived work must preserve the copyright notice and this
* notice unmodified.
*
* Resin Open Source is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* Resin Open Source 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, or any warranty
* of NON-INFRINGEMENT. See the GNU General Public License for more
* details.
*
* You should have received a copy of the GNU General Public License
* along with Resin Open Source; if not, write to the
*
* Free Software Foundation, Inc.
* 59 Temple Place, Suite 330
* Boston, MA 02111-1307 USA
*
* @author Scott Ferguson
*/
package javax.xml.bind.annotation;
public enum XmlAccessType {
FIELD, NONE, PROPERTY, PUBLIC_MEMBER;
}
| mdaniel/svn-caucho-com-resin | modules/jaxb/src/javax/xml/bind/annotation/XmlAccessType.java | Java | gpl-2.0 | 1,117 |
/**
* Copyright (c) 2009--2012 Red Hat, Inc.
*
* This software is licensed to you under the GNU General Public License,
* version 2 (GPLv2). There is NO WARRANTY for this software, express or
* implied, including the implied warranties of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. You should have received a copy of GPLv2
* along with this software; if not, see
* http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt.
*
* Red Hat trademarks are not licensed under GPLv2. No permission is
* granted to use or replicate Red Hat trademarks that are incorporated
* in this software or its documentation.
*/
package com.redhat.rhn.taskomatic.task;
import com.redhat.rhn.common.conf.Config;
import com.redhat.rhn.common.hibernate.HibernateFactory;
import com.redhat.rhn.taskomatic.TaskoRun;
import com.redhat.rhn.taskomatic.task.threaded.TaskQueue;
import com.redhat.rhn.taskomatic.task.threaded.TaskQueueFactory;
import org.apache.log4j.FileAppender;
import org.apache.log4j.Logger;
import org.apache.log4j.PatternLayout;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import java.io.IOException;
/**
* Custom Quartz Job implementation which only allows one thread to
* run at a time. All other threads return without performing any work.
* This policy was chosen instead of blocking so as to reduce threading
* problems inside Quartz itself.
*
* @version $Rev $
*
*/
public abstract class RhnQueueJob implements RhnJob {
private TaskoRun jobRun = null;
protected abstract Logger getLogger();
/**
* {@inheritDoc}
*/
public void appendExceptionToLogError(Exception e) {
getLogger().error(e.getMessage());
getLogger().error(e.getCause());
}
private void logToNewFile() {
PatternLayout pattern =
new PatternLayout(DEFAULT_LOGGING_LAYOUT);
try {
getLogger().removeAllAppenders();
FileAppender appender = new FileAppender(pattern,
jobRun.buildStdOutputLogPath());
getLogger().addAppender(appender);
}
catch (IOException e) {
getLogger().warn("Logging to file disabled");
}
}
/**
* {@inheritDoc}
*/
public void execute(JobExecutionContext ctx, TaskoRun runIn)
throws JobExecutionException {
setJobRun(runIn);
try {
execute(ctx);
}
catch (Exception e) {
if (HibernateFactory.getSession().getTransaction().isActive()) {
HibernateFactory.rollbackTransaction();
HibernateFactory.closeSession();
}
appendExceptionToLogError(e);
jobRun.saveStatus(TaskoRun.STATUS_FAILED);
}
HibernateFactory.commitTransaction();
HibernateFactory.closeSession();
}
/**
* {@inheritDoc}
*/
public void execute(JobExecutionContext ctx)
throws JobExecutionException {
TaskQueueFactory factory = TaskQueueFactory.get();
String queueName = getQueueName();
TaskQueue queue = factory.getQueue(queueName);
if (queue == null) {
try {
queue = factory.createQueue(queueName, getDriverClass(), getLogger());
}
catch (Exception e) {
getLogger().error(e);
return;
}
}
if (queue.changeRun(jobRun)) {
jobRun.start();
HibernateFactory.commitTransaction();
HibernateFactory.closeSession();
logToNewFile();
getLogger().debug("Starting run " + jobRun.getId());
}
else {
// close current run
TaskoRun run = (TaskoRun) HibernateFactory.reload(jobRun);
run.appendToOutputLog("Run with id " + queue.getQueueRun().getId() +
" handles the whole task queue.");
run.skipped();
HibernateFactory.commitTransaction();
HibernateFactory.closeSession();
}
int defaultItems = 3;
if (queueName.equals("channel_repodata")) {
defaultItems = 1;
}
int maxWorkItems = Config.get().getInt("taskomatic." + queueName +
"_max_work_items", defaultItems);
if (queue.getQueueSize() < maxWorkItems) {
queue.run(this);
}
else {
getLogger().debug("Maximum number of workers already put ... skipping.");
}
}
/**
* @return Returns the run.
*/
public TaskoRun getRun() {
return jobRun;
}
/**
* @param runIn The run to set.
*/
public void setJobRun(TaskoRun runIn) {
jobRun = runIn;
}
protected abstract Class getDriverClass();
protected abstract String getQueueName();
}
| hustodemon/spacewalk | java/code/src/com/redhat/rhn/taskomatic/task/RhnQueueJob.java | Java | gpl-2.0 | 4,855 |
///////////////////////////////////////////////////////////////////////////////
// For information as to what this class does, see the Javadoc, below. //
// Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, //
// 2007, 2008, 2009, 2010, 2014, 2015 by Peter Spirtes, Richard Scheines, Joseph //
// Ramsey, and Clark Glymour. //
// //
// This program is free software; you can redistribute it and/or modify //
// it under the terms of the GNU General Public License as published by //
// the Free Software Foundation; either version 2 of the License, or //
// (at your option) any later version. //
// //
// This program 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 General Public License for more details. //
// //
// You should have received a copy of the GNU General Public License //
// along with this program; if not, write to the Free Software //
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA //
///////////////////////////////////////////////////////////////////////////////
package edu.cmu.tetrad.search.mb;
import edu.cmu.tetrad.data.DataSet;
import edu.cmu.tetrad.data.IKnowledge;
import edu.cmu.tetrad.data.Knowledge2;
import edu.cmu.tetrad.graph.EdgeListGraph;
import edu.cmu.tetrad.graph.Graph;
import edu.cmu.tetrad.graph.Node;
import edu.cmu.tetrad.search.FgesOrienter;
import edu.cmu.tetrad.search.GraphSearch;
import edu.cmu.tetrad.search.IndependenceTest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* Implements the MMHC algorithm.
*
* @author Joseph Ramsey (this version).
*/
public class Mmhc implements GraphSearch {
/**
* The independence test used for the PC search.
*/
private IndependenceTest independenceTest;
/**
* The maximum number of nodes conditioned on in the search.
*/
private int depth = Integer.MAX_VALUE;
private DataSet data;
private IKnowledge knowledge = new Knowledge2();
//=============================CONSTRUCTORS==========================//
public Mmhc(IndependenceTest test, DataSet dataSet) {
this.depth = -1;
this.independenceTest = test;
this.data = dataSet;
}
//==============================PUBLIC METHODS========================//
public IndependenceTest getIndependenceTest() {
return independenceTest;
}
public int getDepth() {
return depth;
}
public long getElapsedTime() {
return 0;
}
/**
* Runs PC starting with a fully connected graph over all of the variables in the domain of the independence test.
*/
public Graph search() {
List<Node> variables = independenceTest.getVariables();
Mmmb mmmb = new Mmmb(independenceTest, getDepth(), true);
Map<Node, List<Node>> pc = new HashMap<>();
for (Node x : variables) {
pc.put(x, mmmb.getPc(x));
}
Graph graph = new EdgeListGraph();
for (Node x : variables) {
graph.addNode(x);
}
for (Node x : variables) {
for (Node y : pc.get(x)) {
if (!graph.isAdjacentTo(x, y)) {
graph.addUndirectedEdge(x, y);
}
}
}
FgesOrienter orienter = new FgesOrienter(data);
orienter.orient(graph);
return graph;
}
public IKnowledge getKnowledge() {
return knowledge;
}
public void setKnowledge(IKnowledge knowledge) {
if (knowledge == null) {
throw new NullPointerException();
}
this.knowledge = knowledge;
}
public void setDepth(int depth) {
this.depth = depth;
}
}
| ekummerfeld/GdistanceP | tetrad-lib/src/main/java/edu/cmu/tetrad/search/mb/Mmhc.java | Java | gpl-2.0 | 4,266 |
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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.
*/
/**
* @author Vera Y. Petrashkova
*/
package org.apache.harmony.security.tests.java.security.cert;
import java.security.cert.CertificateExpiredException;
import junit.framework.TestCase;
/**
* Tests for <code>DigestException</code> class constructors and methods.
*
*/
public class CertificateExpiredExceptionTest extends TestCase {
public static void main(String[] args) {
}
/**
* Constructor for CertificateExpiredExceptionTests.
*
* @param arg0
*/
public CertificateExpiredExceptionTest(String arg0) {
super(arg0);
}
static String[] msgs = {
"",
"Check new message",
"Check new message Check new message Check new message Check new message Check new message" };
static Throwable tCause = new Throwable("Throwable for exception");
/**
* Test for <code>CertificateExpiredException()</code> constructor
* Assertion: constructs CertificateExpiredException with no detail message
*/
public void testCertificateExpiredException01() {
CertificateExpiredException tE = new CertificateExpiredException();
assertNull("getMessage() must return null.", tE.getMessage());
assertNull("getCause() must return null", tE.getCause());
}
/**
* Test for <code>CertificateExpiredException(String)</code> constructor
* Assertion: constructs CertificateExpiredException with detail message
* msg. Parameter <code>msg</code> is not null.
*/
public void testCertificateExpiredException02() {
CertificateExpiredException tE;
for (int i = 0; i < msgs.length; i++) {
tE = new CertificateExpiredException(msgs[i]);
assertEquals("getMessage() must return: ".concat(msgs[i]), tE
.getMessage(), msgs[i]);
assertNull("getCause() must return null", tE.getCause());
}
}
/**
* Test for <code>CertificateExpiredException(String)</code> constructor
* Assertion: constructs CertificateExpiredException when <code>msg</code>
* is null
*/
public void testCertificateExpiredException03() {
String msg = null;
CertificateExpiredException tE = new CertificateExpiredException(msg);
assertNull("getMessage() must return null.", tE.getMessage());
assertNull("getCause() must return null", tE.getCause());
}
}
| rex-xxx/mt6572_x201 | external/apache-harmony/security/src/test/api/java/org/apache/harmony/security/tests/java/security/cert/CertificateExpiredExceptionTest.java | Java | gpl-2.0 | 3,216 |
/*
* Copyright (c) 1998-2012 Caucho Technology -- all rights reserved
*
* This file is part of Resin(R) Open Source
*
* Each copy or derived work must preserve the copyright notice and this
* notice unmodified.
*
* Resin Open Source is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* Resin Open Source 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, or any warranty
* of NON-INFRINGEMENT. See the GNU General Public License for more
* details.
*
* You should have received a copy of the GNU General Public License
* along with Resin Open Source; if not, write to the
* Free SoftwareFoundation, Inc.
* 59 Temple Place, Suite 330
* Boston, MA 02111-1307 USA
*
* @author Scott Ferguson
*/
package com.caucho.ejb.cfg;
import com.caucho.config.ConfigException;
import com.caucho.config.types.DescriptionGroupConfig;
import com.caucho.config.types.Signature;
import com.caucho.util.L10N;
import com.caucho.vfs.Path;
import java.util.*;
import javax.annotation.PostConstruct;
/**
* Configuration for an ejb bean.
*/
public class EjbJar extends DescriptionGroupConfig {
private static final L10N L = new L10N(EjbJar.class);
private final EjbConfig _config;
private String _ejbModuleName;
private Path _rootPath;
private boolean _isMetadataComplete;
private boolean _isSkip;
public EjbJar(EjbConfig config,
String ejbModuleName,
Path rootPath)
{
_config = config;
_ejbModuleName = ejbModuleName;
_rootPath = rootPath;
}
public String getModuleName()
{
return _ejbModuleName;
}
public void setModuleName(String moduleName)
{
_ejbModuleName = moduleName;
}
public void setVersion(String version)
{
}
public void setSchemaLocation(String value)
{
}
public void setSkip(boolean isSkip)
{
_isSkip = isSkip;
}
public boolean isSkip()
{
return _isSkip;
}
public void setMetadataComplete(boolean isMetadataComplete)
{
_isMetadataComplete = isMetadataComplete;
}
public boolean isMetadataComplete()
{
return _isMetadataComplete;
}
public EjbEnterpriseBeans createEnterpriseBeans()
throws ConfigException
{
return new EjbEnterpriseBeans(_config, this, _ejbModuleName);
}
public InterceptorsConfig createInterceptors()
throws ConfigException
{
return new InterceptorsConfig(_config);
}
public Relationships createRelationships()
throws ConfigException
{
return new Relationships(_config);
}
public AssemblyDescriptor createAssemblyDescriptor()
throws ConfigException
{
return new AssemblyDescriptor(this, _config);
}
public void addQueryFunction(QueryFunction fun)
{
}
public void setBooleanLiteral(BooleanLiteral literal)
{
}
ContainerTransaction createContainerTransaction()
{
return new ContainerTransaction(this, _config);
}
MethodPermission createMethodPermission()
{
return new MethodPermission(_config);
}
public String toString()
{
return getClass().getSimpleName() + "[" + _rootPath.getFullPath() + "]";
}
public class MethodPermission {
EjbConfig _config;
MethodSignature _method;
ArrayList<String> _roles;
MethodPermission(EjbConfig config)
{
_config = config;
}
public void setDescription(String description)
{
}
public void setUnchecked(boolean unchecked)
{
}
public void setRoleName(String roleName)
{
if (_roles == null)
_roles = new ArrayList<String>();
_roles.add(roleName);
}
public void setMethod(MethodSignature method)
{
_method = method;
}
@PostConstruct
public void init()
throws ConfigException
{
if (isSkip())
return;
EjbBean bean = _config.getBeanConfig(_method.getEJBName());
if (bean == null)
throw new ConfigException(L.l("'{0}' is an unknown bean.",
_method.getEJBName()));
EjbMethodPattern method = bean.createMethod(_method);
if (_roles != null)
method.setRoles(_roles);
}
}
public static class QueryFunction {
FunctionSignature _sig;
String _sql;
public void setSignature(Signature sig)
throws ConfigException
{
_sig = new FunctionSignature(sig.getSignature());
}
public FunctionSignature getSignature()
{
return _sig;
}
public void setSQL(String sql)
throws ConfigException
{
_sql = sql;
}
public String getSQL()
{
return _sql;
}
@PostConstruct
public void init()
{
_sig.setSQL(_sql);
}
}
public static class Relationships {
EjbConfig _config;
Relationships(EjbConfig config)
{
_config = config;
}
}
}
| WelcomeHUME/svn-caucho-com-resin | modules/resin/src/com/caucho/ejb/cfg/EjbJar.java | Java | gpl-2.0 | 5,125 |
package emu.project64.input;
import java.util.Set;
import emu.project64.AndroidDevice;
import emu.project64.input.map.TouchMap;
import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.graphics.Point;
import android.os.Vibrator;
import android.util.SparseIntArray;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
/**
* A class for generating N64 controller commands from a touchscreen.
*/
public class TouchController extends AbstractController implements OnTouchListener
{
public interface OnStateChangedListener
{
/**
* Called after the analog stick values have changed.
*
* @param axisFractionX The x-axis fraction, between -1 and 1, inclusive.
* @param axisFractionY The y-axis fraction, between -1 and 1, inclusive.
*/
public void onAnalogChanged( float axisFractionX, float axisFractionY );
/**
* Called after auto-hold button state changed.
*
* @param pressed The auto-hold state.
* @param index The index of the auto-hold mask.
*/
public void onAutoHold( boolean pressed, int index );
}
public static final int AUTOHOLD_METHOD_DISABLED = 0;
public static final int AUTOHOLD_METHOD_LONGPRESS = 1;
public static final int AUTOHOLD_METHOD_SLIDEOUT = 2;
/** The number of milliseconds to wait before auto-holding (long-press method). */
private static final int AUTOHOLD_LONGPRESS_TIME = 1000;
/** The pattern vibration when auto-hold is engaged. */
private static final long[] AUTOHOLD_VIBRATE_PATTERN = { 0, 50, 50, 50 };
/** The number of milliseconds of vibration when pressing a key. */
private static final int FEEDBACK_VIBRATE_TIME = 50;
/** The maximum number of pointers to query. */
private static final int MAX_POINTER_IDS = 256;
/** The state change listener. */
private final OnStateChangedListener mListener;
/** The map from screen coordinates to N64 controls. */
private final TouchMap mTouchMap;
/** The map from pointer ids to N64 controls. */
private final SparseIntArray mPointerMap = new SparseIntArray();
/** The method used for auto-holding buttons. */
private final int mAutoHoldMethod;
/** The set of auto-holdable buttons. */
private final Set<Integer> mAutoHoldables;
/** Whether touchscreen feedback is enabled. */
private final boolean mTouchscreenFeedback;
/** The touch state of each pointer. True indicates down, false indicates up. */
private final boolean[] mTouchState = new boolean[MAX_POINTER_IDS];
/** The x-coordinate of each pointer, between 0 and (screenwidth-1), inclusive. */
private final int[] mPointerX = new int[MAX_POINTER_IDS];
/** The y-coordinate of each pointer, between 0 and (screenheight-1), inclusive. */
private final int[] mPointerY = new int[MAX_POINTER_IDS];
/** The pressed start time of each pointer. */
private final long[] mStartTime = new long[MAX_POINTER_IDS];
/** The time between press and release of each pointer. */
private final long[] mElapsedTime = new long[MAX_POINTER_IDS];
/**
* The identifier of the pointer associated with the analog stick. -1 indicates the stick has
* been released.
*/
private int mAnalogPid = -1;
/** The touch event source to listen to, or 0 to listen to all sources. */
private int mSourceFilter = 0;
private Vibrator mVibrator = null;
/**
* Instantiates a new touch controller.
*
* @param touchMap The map from touch coordinates to N64 controls.
* @param view The view receiving touch event data.
* @param listener The listener for controller state changes.
* @param vibrator The haptic feedback device. MUST BE NULL if vibrate permission not granted.
* @param autoHoldMethod The method for auto-holding buttons.
* @param touchscreenFeedback True if haptic feedback should be used.
* @param autoHoldableButtons The N64 commands that correspond to auto-holdable buttons.
*/
public TouchController( TouchMap touchMap, View view, OnStateChangedListener listener,
Vibrator vibrator, int autoHoldMethod, boolean touchscreenFeedback,
Set<Integer> autoHoldableButtons )
{
mListener = listener;
mTouchMap = touchMap;
mVibrator = vibrator;
mAutoHoldMethod = autoHoldMethod;
mTouchscreenFeedback = touchscreenFeedback;
mAutoHoldables = autoHoldableButtons;
view.setOnTouchListener( this );
}
/**
* Sets the touch event source filter.
*
* @param source The source to listen to, or 0 to listen to all sources.
*/
public void setSourceFilter( int source )
{
mSourceFilter = source;
}
/*
* (non-Javadoc)
*
* @see android.view.View.OnTouchListener#onTouch(android.view.View, android.view.MotionEvent)
*/
@SuppressLint( "ClickableViewAccessibility" )
@Override
@TargetApi( 9 )
public boolean onTouch( View view, MotionEvent event )
{
// Filter by source, if applicable
int source = AndroidDevice.IS_GINGERBREAD ? event.getSource() : 0;
if( mSourceFilter != 0 && mSourceFilter != source )
return false;
int action = event.getAction();
int actionCode = action & MotionEvent.ACTION_MASK;
int pid = -1;
switch( actionCode )
{
case MotionEvent.ACTION_POINTER_DOWN:
// A non-primary touch has been made
pid = event.getPointerId( action >> MotionEvent.ACTION_POINTER_INDEX_SHIFT );
mStartTime[pid] = System.currentTimeMillis();
mTouchState[pid] = true;
break;
case MotionEvent.ACTION_POINTER_UP:
// A non-primary touch has been released
pid = event.getPointerId( action >> MotionEvent.ACTION_POINTER_INDEX_SHIFT );
mElapsedTime[pid] = System.currentTimeMillis() - mStartTime[pid];
mTouchState[pid] = false;
break;
case MotionEvent.ACTION_DOWN:
// A touch gesture has started (e.g. analog stick movement)
for( int i = 0; i < event.getPointerCount(); i++ )
{
pid = event.getPointerId( i );
mStartTime[pid] = System.currentTimeMillis();
mTouchState[pid] = true;
}
break;
case MotionEvent.ACTION_UP:
case MotionEvent.ACTION_CANCEL:
// A touch gesture has ended or canceled (e.g. analog stick movement)
for( int i = 0; i < event.getPointerCount(); i++ )
{
pid = event.getPointerId( i );
mElapsedTime[pid] = System.currentTimeMillis() - mStartTime[pid];
mTouchState[pid] = false;
}
break;
default:
break;
}
// Update the coordinates of down pointers and record max PID for speed
int maxPid = -1;
for( int i = 0; i < event.getPointerCount(); i++ )
{
pid = event.getPointerId( i );
if( pid > maxPid )
maxPid = pid;
if( mTouchState[pid] )
{
mPointerX[pid] = (int) event.getX( i );
mPointerY[pid] = (int) event.getY( i );
}
}
// Process each touch
processTouches( mTouchState, mPointerX, mPointerY, mElapsedTime, maxPid );
return true;
}
/**
* Sets the N64 controller state based on where the screen is (multi-) touched. Values outside
* the ranges listed below are safe.
*
* @param touchstate The touch state of each pointer. True indicates down, false indicates up.
* @param pointerX The x-coordinate of each pointer, between 0 and (screenwidth-1), inclusive.
* @param pointerY The y-coordinate of each pointer, between 0 and (screenheight-1), inclusive.
* @param maxPid Maximum ID of the pointers that have changed (speed optimization).
*/
private void processTouches( boolean[] touchstate, int[] pointerX, int[] pointerY,
long[] elapsedTime, int maxPid )
{
boolean analogMoved = false;
// Process each pointer in sequence
for( int pid = 0; pid <= maxPid; pid++ )
{
// Release analog if its pointer is not touching the screen
if( pid == mAnalogPid && !touchstate[pid] )
{
analogMoved = true;
mAnalogPid = -1;
mState.axisFractionX = 0;
mState.axisFractionY = 0;
}
// Process button inputs
if( pid != mAnalogPid )
processButtonTouch( touchstate[pid], pointerX[pid], pointerY[pid],
elapsedTime[pid], pid );
// Process analog inputs
if( touchstate[pid] && processAnalogTouch( pid, pointerX[pid], pointerY[pid] ) )
analogMoved = true;
}
// Call the super method to send the input to the core
notifyChanged();
// Update the skin if the virtual analog stick moved
if( analogMoved && mListener != null )
mListener.onAnalogChanged( mState.axisFractionX, mState.axisFractionY );
}
/**
* Process a touch as if intended for a button. Values outside the ranges listed below are safe.
*
* @param touched Whether the button is pressed or not.
* @param xLocation The x-coordinate of the touch, between 0 and (screenwidth-1), inclusive.
* @param yLocation The y-coordinate of the touch, between 0 and (screenheight-1), inclusive.
* @param pid The identifier of the touch pointer.
*/
private void processButtonTouch( boolean touched, int xLocation, int yLocation,
long timeElapsed, int pid )
{
// Determine the index of the button that was pressed
int index = touched
? mTouchMap.getButtonPress( xLocation, yLocation )
: mPointerMap.get( pid, TouchMap.UNMAPPED );
// Update the pointer map
if( !touched )
{
// Finger lifted off screen, forget what this pointer was touching
mPointerMap.delete( pid );
}
else
{
// Determine where the finger came from if is was slid
int prevIndex = mPointerMap.get( pid, TouchMap.UNMAPPED );
// Finger touched somewhere on screen, remember what this pointer is touching
mPointerMap.put( pid, index );
if( prevIndex != index )
{
// Finger slid from somewhere else, act accordingly
// There are three possibilities:
// - old button --> new button
// - nothing --> new button
// - old button --> nothing
// Reset this pointer's start time
mStartTime[pid] = System.currentTimeMillis();
if( prevIndex != TouchMap.UNMAPPED )
{
// Slid off a valid button
if( !isAutoHoldable( prevIndex ) || mAutoHoldMethod == AUTOHOLD_METHOD_DISABLED )
{
// Slid off a non-auto-hold button
setTouchState( prevIndex, false );
}
else
{
// Slid off an auto-hold button
switch( mAutoHoldMethod )
{
case AUTOHOLD_METHOD_LONGPRESS:
// Using long-press method, release auto-hold button
if( mListener != null )
mListener.onAutoHold( false, prevIndex );
setTouchState( prevIndex, false );
break;
case AUTOHOLD_METHOD_SLIDEOUT:
// Using slide-off method, engage auto-hold button
if( mVibrator != null )
{
mVibrator.cancel();
mVibrator.vibrate( AUTOHOLD_VIBRATE_PATTERN, -1 );
}
if( mListener != null )
mListener.onAutoHold( true, prevIndex );
setTouchState( prevIndex, true );
break;
}
}
}
}
}
if( index != TouchMap.UNMAPPED )
{
// Finger is on a valid button
// Provide simple vibration feedback for any valid button when first touched
if( touched && mTouchscreenFeedback && mVibrator != null )
{
boolean firstTouched;
if( index < NUM_N64_BUTTONS )
{
// Single button pressed
firstTouched = !mState.buttons[index];
}
else
{
// Two d-pad buttons pressed simultaneously
switch( index )
{
case TouchMap.DPD_RU:
firstTouched = !( mState.buttons[DPD_R] && mState.buttons[DPD_U] );
break;
case TouchMap.DPD_RD:
firstTouched = !( mState.buttons[DPD_R] && mState.buttons[DPD_D] );
break;
case TouchMap.DPD_LD:
firstTouched = !( mState.buttons[DPD_L] && mState.buttons[DPD_D] );
break;
case TouchMap.DPD_LU:
firstTouched = !( mState.buttons[DPD_L] && mState.buttons[DPD_U] );
break;
default:
firstTouched = false;
break;
}
}
if( firstTouched )
{
mVibrator.cancel();
mVibrator.vibrate( FEEDBACK_VIBRATE_TIME );
}
}
// Set the controller state accordingly
if( touched || !isAutoHoldable( index ) || mAutoHoldMethod == AUTOHOLD_METHOD_DISABLED )
{
// Finger just touched a button (any kind) OR
// Finger just lifted off non-auto-holdable button
setTouchState( index, touched );
// Do not provide auto-hold feedback yet
}
else
{
// Finger just lifted off an auto-holdable button
switch( mAutoHoldMethod )
{
case AUTOHOLD_METHOD_SLIDEOUT:
// Release auto-hold button if using slide-off method
if( mListener != null )
mListener.onAutoHold( false, index );
setTouchState( index, false );
break;
case AUTOHOLD_METHOD_LONGPRESS:
if( timeElapsed < AUTOHOLD_LONGPRESS_TIME )
{
// Release auto-hold if short-pressed
if( mListener != null )
mListener.onAutoHold( false, index );
setTouchState( index, false );
}
else
{
// Engage auto-hold if long-pressed
if( mVibrator != null )
{
mVibrator.cancel();
mVibrator.vibrate( AUTOHOLD_VIBRATE_PATTERN, -1 );
}
if( mListener != null )
mListener.onAutoHold( true, index );
setTouchState( index, true );
}
break;
}
}
}
}
/**
* Checks if the button mapped to an N64 command is auto-holdable.
*
* @param commandIndex The index to the N64 command.
*
* @return True if the button mapped to the command is auto-holdable.
*/
private boolean isAutoHoldable( int commandIndex )
{
return mAutoHoldables != null && mAutoHoldables.contains( commandIndex );
}
/**
* Sets the state of a button, and handles the D-Pad diagonals.
*
* @param index Which button is affected.
* @param touched Whether the button is pressed or not.
*/
private void setTouchState( int index, boolean touched )
{
// Set the button state
if( index < AbstractController.NUM_N64_BUTTONS )
{
// A single button was pressed
mState.buttons[index] = touched;
}
else
{
// Two d-pad buttons pressed simultaneously
switch( index )
{
case TouchMap.DPD_RU:
mState.buttons[DPD_R] = touched;
mState.buttons[DPD_U] = touched;
break;
case TouchMap.DPD_RD:
mState.buttons[DPD_R] = touched;
mState.buttons[DPD_D] = touched;
break;
case TouchMap.DPD_LD:
mState.buttons[DPD_L] = touched;
mState.buttons[DPD_D] = touched;
break;
case TouchMap.DPD_LU:
mState.buttons[DPD_L] = touched;
mState.buttons[DPD_U] = touched;
break;
default:
break;
}
}
}
/**
* Process a touch as if intended for the analog stick. Values outside the ranges listed below
* are safe.
*
* @param pointerId The pointer identifier.
* @param xLocation The x-coordinate of the touch, between 0 and (screenwidth-1), inclusive.
* @param yLocation The y-coordinate of the touch, between 0 and (screenheight-1), inclusive.
*
* @return True, if the analog state changed.
*/
private boolean processAnalogTouch( int pointerId, int xLocation, int yLocation )
{
// Get the cartesian displacement of the analog stick
Point point = mTouchMap.getAnalogDisplacement( xLocation, yLocation );
// Compute the pythagorean displacement of the stick
int dX = point.x;
int dY = point.y;
float displacement = (float) Math.sqrt( ( dX * dX ) + ( dY * dY ) );
// "Capture" the analog control
if( mTouchMap.isInCaptureRange( displacement ) )
mAnalogPid = pointerId;
if( pointerId == mAnalogPid )
{
// User is controlling the analog stick
// Limit range of motion to an octagon (like the real N64 controller)
point = mTouchMap.getConstrainedDisplacement( dX, dY );
dX = point.x;
dY = point.y;
displacement = (float) Math.sqrt( ( dX * dX ) + ( dY * dY ) );
// Fraction of full-throttle, between 0 and 1, inclusive
float p = mTouchMap.getAnalogStrength( displacement );
// Store the axis values in the super fields (screen y is inverted)
mState.axisFractionX = p * dX / displacement;
mState.axisFractionY = -p * dY / displacement;
// Analog state changed
return true;
}
// Analog state did not change
return false;
}
}
| shygoo/project64 | Android/app/src/main/java/emu/project64/input/TouchController.java | Java | gpl-2.0 | 20,746 |
/*
* Copyright 2008 ZXing 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 com.google.zxing.aztec;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.common.AbstractBlackBoxTestCase;
/**
* @author David Olivier
*/
public final class AztecBlackBox1TestCase extends AbstractBlackBoxTestCase {
public AztecBlackBox1TestCase() {
super("test/data/blackbox/aztec-1", new AztecReader(), BarcodeFormat.AZTEC);
addTest(12, 12, 0.0f);
addTest(12, 12, 90.0f);
addTest(12, 12, 180.0f);
addTest(12, 12, 270.0f);
}
} | faarwa/EngSocP5 | zxing/core/test/src/com/google/zxing/aztec/AztecBlackBox1TestCase.java | Java | gpl-3.0 | 1,076 |
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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.apache.commons.io.filefilter;
import java.io.File;
import java.io.Serializable;
import java.util.List;
import org.apache.commons.io.IOCase;
/**
* Filters filenames for a certain prefix.
* <p>
* For example, to print all files and directories in the
* current directory whose name starts with <code>Test</code>:
*
* <pre>
* File dir = new File(".");
* String[] files = dir.list( new PrefixFileFilter("Test") );
* for ( int i = 0; i < files.length; i++ ) {
* System.out.println(files[i]);
* }
* </pre>
*
* @since Commons IO 1.0
* @version $Revision: 1005099 $ $Date: 2010-10-06 17:13:01 +0100 (Wed, 06 Oct 2010) $
*
* @author Stephen Colebourne
* @author Federico Barbieri
* @author Serge Knystautas
* @author Peter Donald
* @see FileFilterUtils#prefixFileFilter(String)
* @see FileFilterUtils#prefixFileFilter(String, IOCase)
*/
public class PrefixFileFilter extends AbstractFileFilter implements Serializable {
/** The filename prefixes to search for */
private final String[] prefixes;
/** Whether the comparison is case sensitive. */
private final IOCase caseSensitivity;
/**
* Constructs a new Prefix file filter for a single prefix.
*
* @param prefix the prefix to allow, must not be null
* @throws IllegalArgumentException if the prefix is null
*/
public PrefixFileFilter(String prefix) {
this(prefix, IOCase.SENSITIVE);
}
/**
* Constructs a new Prefix file filter for a single prefix
* specifying case-sensitivity.
*
* @param prefix the prefix to allow, must not be null
* @param caseSensitivity how to handle case sensitivity, null means case-sensitive
* @throws IllegalArgumentException if the prefix is null
* @since Commons IO 1.4
*/
public PrefixFileFilter(String prefix, IOCase caseSensitivity) {
if (prefix == null) {
throw new IllegalArgumentException("The prefix must not be null");
}
this.prefixes = new String[] {prefix};
this.caseSensitivity = (caseSensitivity == null ? IOCase.SENSITIVE : caseSensitivity);
}
/**
* Constructs a new Prefix file filter for any of an array of prefixes.
* <p>
* The array is not cloned, so could be changed after constructing the
* instance. This would be inadvisable however.
*
* @param prefixes the prefixes to allow, must not be null
* @throws IllegalArgumentException if the prefix array is null
*/
public PrefixFileFilter(String[] prefixes) {
this(prefixes, IOCase.SENSITIVE);
}
/**
* Constructs a new Prefix file filter for any of an array of prefixes
* specifying case-sensitivity.
* <p>
* The array is not cloned, so could be changed after constructing the
* instance. This would be inadvisable however.
*
* @param prefixes the prefixes to allow, must not be null
* @param caseSensitivity how to handle case sensitivity, null means case-sensitive
* @throws IllegalArgumentException if the prefix is null
* @since Commons IO 1.4
*/
public PrefixFileFilter(String[] prefixes, IOCase caseSensitivity) {
if (prefixes == null) {
throw new IllegalArgumentException("The array of prefixes must not be null");
}
this.prefixes = new String[prefixes.length];
System.arraycopy(prefixes, 0, this.prefixes, 0, prefixes.length);
this.caseSensitivity = (caseSensitivity == null ? IOCase.SENSITIVE : caseSensitivity);
}
/**
* Constructs a new Prefix file filter for a list of prefixes.
*
* @param prefixes the prefixes to allow, must not be null
* @throws IllegalArgumentException if the prefix list is null
* @throws ClassCastException if the list does not contain Strings
*/
public PrefixFileFilter(List<String> prefixes) {
this(prefixes, IOCase.SENSITIVE);
}
/**
* Constructs a new Prefix file filter for a list of prefixes
* specifying case-sensitivity.
*
* @param prefixes the prefixes to allow, must not be null
* @param caseSensitivity how to handle case sensitivity, null means case-sensitive
* @throws IllegalArgumentException if the prefix list is null
* @throws ClassCastException if the list does not contain Strings
* @since Commons IO 1.4
*/
public PrefixFileFilter(List<String> prefixes, IOCase caseSensitivity) {
if (prefixes == null) {
throw new IllegalArgumentException("The list of prefixes must not be null");
}
this.prefixes = prefixes.toArray(new String[prefixes.size()]);
this.caseSensitivity = (caseSensitivity == null ? IOCase.SENSITIVE : caseSensitivity);
}
/**
* Checks to see if the filename starts with the prefix.
*
* @param file the File to check
* @return true if the filename starts with one of our prefixes
*/
@Override
public boolean accept(File file) {
String name = file.getName();
for (String prefix : this.prefixes) {
if (caseSensitivity.checkStartsWith(name, prefix)) {
return true;
}
}
return false;
}
/**
* Checks to see if the filename starts with the prefix.
*
* @param file the File directory
* @param name the filename
* @return true if the filename starts with one of our prefixes
*/
@Override
public boolean accept(File file, String name) {
for (String prefix : prefixes) {
if (caseSensitivity.checkStartsWith(name, prefix)) {
return true;
}
}
return false;
}
/**
* Provide a String representaion of this file filter.
*
* @return a String representaion
*/
@Override
public String toString() {
StringBuilder buffer = new StringBuilder();
buffer.append(super.toString());
buffer.append("(");
if (prefixes != null) {
for (int i = 0; i < prefixes.length; i++) {
if (i > 0) {
buffer.append(",");
}
buffer.append(prefixes[i]);
}
}
buffer.append(")");
return buffer.toString();
}
}
| tr4656/Hungry | src/org/apache/commons/io/filefilter/PrefixFileFilter.java | Java | gpl-3.0 | 7,155 |
/**
* This file is part of muCommander, http://www.mucommander.com
* Copyright (C) 2002-2010 Maxence Bernard
*
* muCommander 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 3 of the License, or
* (at your option) any later version.
*
* muCommander 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 program. If not, see <http://www.gnu.org/licenses/>.
*/
package com.mucommander.commons.file.filter;
/**
* <code>PathFilter</code> is a {@link FileFilter} that operates on absolute file paths.
*
* @see AbstractPathFilter
* @author Maxence Bernard
*/
public interface PathFilter extends StringCriterionFilter {
}
| jorgevasquezp/mucommander | src/main/com/mucommander/commons/file/filter/PathFilter.java | Java | gpl-3.0 | 1,047 |
/*******************************************************************************
* Copyright (c) 2000, 2003 IBM Corporation and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Common Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/cpl-v10.html
*
* Contributors:
* IBM Corporation - initial API and implementation
*******************************************************************************/
package org.eclipse.jdt.internal.codeassist.impl;
import java.util.Iterator;
import java.util.Map;
import org.eclipse.jdt.core.compiler.CharOperation;
public class AssistOptions {
/**
* Option IDs
*/
public static final String OPTION_PerformVisibilityCheck =
"org.eclipse.jdt.core.codeComplete.visibilityCheck"; //$NON-NLS-1$
public static final String OPTION_ForceImplicitQualification =
"org.eclipse.jdt.core.codeComplete.forceImplicitQualification"; //$NON-NLS-1$
public static final String OPTION_FieldPrefixes =
"org.eclipse.jdt.core.codeComplete.fieldPrefixes"; //$NON-NLS-1$
public static final String OPTION_StaticFieldPrefixes =
"org.eclipse.jdt.core.codeComplete.staticFieldPrefixes"; //$NON-NLS-1$
public static final String OPTION_LocalPrefixes =
"org.eclipse.jdt.core.codeComplete.localPrefixes"; //$NON-NLS-1$
public static final String OPTION_ArgumentPrefixes =
"org.eclipse.jdt.core.codeComplete.argumentPrefixes"; //$NON-NLS-1$
public static final String OPTION_FieldSuffixes =
"org.eclipse.jdt.core.codeComplete.fieldSuffixes"; //$NON-NLS-1$
public static final String OPTION_StaticFieldSuffixes =
"org.eclipse.jdt.core.codeComplete.staticFieldSuffixes"; //$NON-NLS-1$
public static final String OPTION_LocalSuffixes =
"org.eclipse.jdt.core.codeComplete.localSuffixes"; //$NON-NLS-1$
public static final String OPTION_ArgumentSuffixes =
"org.eclipse.jdt.core.codeComplete.argumentSuffixes"; //$NON-NLS-1$
public static final String ENABLED = "enabled"; //$NON-NLS-1$
public static final String DISABLED = "disabled"; //$NON-NLS-1$
public boolean checkVisibility = false;
public boolean forceImplicitQualification = false;
public char[][] fieldPrefixes = null;
public char[][] staticFieldPrefixes = null;
public char[][] localPrefixes = null;
public char[][] argumentPrefixes = null;
public char[][] fieldSuffixes = null;
public char[][] staticFieldSuffixes = null;
public char[][] localSuffixes = null;
public char[][] argumentSuffixes = null;
/**
* Initializing the assist options with default settings
*/
public AssistOptions() {
// Initializing the assist options with default settings
}
/**
* Initializing the assist options with external settings
*/
public AssistOptions(Map settings) {
if (settings == null)
return;
// filter options which are related to the assist component
Iterator entries = settings.entrySet().iterator();
while (entries.hasNext()) {
Map.Entry entry = (Map.Entry)entries.next();
if (!(entry.getKey() instanceof String))
continue;
if (!(entry.getValue() instanceof String))
continue;
String optionID = (String) entry.getKey();
String optionValue = (String) entry.getValue();
if (optionID.equals(OPTION_PerformVisibilityCheck)) {
if (optionValue.equals(ENABLED)) {
this.checkVisibility = true;
} else
if (optionValue.equals(DISABLED)) {
this.checkVisibility = false;
}
continue;
} else if (optionID.equals(OPTION_ForceImplicitQualification)) {
if (optionValue.equals(ENABLED)) {
this.forceImplicitQualification = true;
} else
if (optionValue.equals(DISABLED)) {
this.forceImplicitQualification = false;
}
continue;
} else if(optionID.equals(OPTION_FieldPrefixes)){
if (optionValue.length() == 0) {
this.fieldPrefixes = null;
} else {
this.fieldPrefixes = CharOperation.splitAndTrimOn(',', optionValue.toCharArray());
}
continue;
} else if(optionID.equals(OPTION_StaticFieldPrefixes)){
if (optionValue.length() == 0) {
this.staticFieldPrefixes = null;
} else {
this.staticFieldPrefixes = CharOperation.splitAndTrimOn(',', optionValue.toCharArray());
}
continue;
} else if(optionID.equals(OPTION_LocalPrefixes)){
if (optionValue.length() == 0) {
this.localPrefixes = null;
} else {
this.localPrefixes = CharOperation.splitAndTrimOn(',', optionValue.toCharArray());
}
continue;
} else if(optionID.equals(OPTION_ArgumentPrefixes)){
if (optionValue.length() == 0) {
this.argumentPrefixes = null;
} else {
this.argumentPrefixes = CharOperation.splitAndTrimOn(',', optionValue.toCharArray());
}
continue;
} else if(optionID.equals(OPTION_FieldSuffixes)){
if (optionValue.length() == 0) {
this.fieldSuffixes = null;
} else {
this.fieldSuffixes = CharOperation.splitAndTrimOn(',', optionValue.toCharArray());
}
continue;
} else if(optionID.equals(OPTION_StaticFieldSuffixes)){
if (optionValue.length() == 0) {
this.staticFieldSuffixes = null;
} else {
this.staticFieldSuffixes = CharOperation.splitAndTrimOn(',', optionValue.toCharArray());
}
continue;
} else if(optionID.equals(OPTION_LocalSuffixes)){
if (optionValue.length() == 0) {
this.localSuffixes = null;
} else {
this.localSuffixes = CharOperation.splitAndTrimOn(',', optionValue.toCharArray());
}
continue;
} else if(optionID.equals(OPTION_ArgumentSuffixes)){
if (optionValue.length() == 0) {
this.argumentSuffixes = null;
} else {
this.argumentSuffixes = CharOperation.splitAndTrimOn(',', optionValue.toCharArray());
}
continue;
}
}
}
} | Niky4000/UsefulUtils | projects/others/eclipse-platform-parent/eclipse.jdt.core-master/org.eclipse.jdt.core.tests.model/workspace/Formatter/test133/A_in.java | Java | gpl-3.0 | 5,799 |
/**
* Copyright (C) 2013 - 2016 Johannes Taelman
*
* This file is part of Axoloti.
*
* Axoloti is free software: you can redistribute it and/or modify it under the
* terms of the GNU General Public License as published by the Free Software
* Foundation, either version 3 of the License, or (at your option) any later
* version.
*
* Axoloti 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with
* Axoloti. If not, see <http://www.gnu.org/licenses/>.
*/
package axoloti.menus;
import axoloti.MainFrame;
import java.io.File;
import java.io.FilenameFilter;
import java.util.Arrays;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
/**
*
* @author jtaelman
*/
public class PopulatePatchMenu {
static void PopulatePatchMenu(JMenu parent, String path, String ext) {
File dir = new File(path);
if (!dir.isDirectory()) {
JMenuItem mi = new JMenuItem("no help patches found");
mi.setEnabled(false);
parent.add(mi);
return;
}
final String extension = ext;
File[] files = dir.listFiles(new java.io.FileFilter() {
@Override
public boolean accept(File pathname) {
return pathname.isDirectory();
}
});
Arrays.sort(files);
for (File subdir : files) {
JMenu fm = new JMenu(subdir.getName());
PopulatePatchMenu(fm, subdir.getPath(), extension);
if (fm.getItemCount() > 0) {
parent.add(fm);
}
}
String filenames[] = dir.list(new FilenameFilter() {
@Override
public boolean accept(File dir, String name) {
return (name.endsWith(extension));
}
});
Arrays.sort(filenames);
for (String fn : filenames) {
String fn2 = fn.substring(0, fn.length() - 4);
JMenuItem fm = new JMenuItem(fn2);
fm.setActionCommand("open:" + path + File.separator + fn);
fm.addActionListener(MainFrame.mainframe);
parent.add(fm);
}
}
}
| dkmorb/axoloti | src/main/java/axoloti/menus/PopulatePatchMenu.java | Java | gpl-3.0 | 2,367 |
/**
* L2FProd.com Common Components 7.3 License.
*
* Copyright 2005-2007 L2FProd.com
*
* 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 com.l2fprod.common.swing.plaf;
/**
* Each new component type of the library will contribute an addon to
* the LookAndFeelAddons. A <code>ComponentAddon</code> is the
* equivalent of a {@link javax.swing.LookAndFeel}but focused on one
* component. <br>
*
* @author <a href="mailto:fred@L2FProd.com">Frederic Lavigne</a>
*/
public interface ComponentAddon {
/**
* @return the name of this addon
*/
String getName();
/**
* Initializes this addon (i.e register UI classes, colors, fonts,
* borders, any UIResource used by the component class). When
* initializing, the addon can register different resources based on
* the addon or the current look and feel.
*
* @param addon the current addon
*/
void initialize(LookAndFeelAddons addon);
/**
* Uninitializes this addon.
*
* @param addon
*/
void uninitialize(LookAndFeelAddons addon);
} | pabalexa/calibre2opds | OpdsOutput/src/main/java/com/l2fprod/common/swing/plaf/ComponentAddon.java | Java | gpl-3.0 | 1,604 |
/*
* Copyright (c) 2011 Matthew Francis
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package org.itadaki.bzip2;
/**
* An in-place, length restricted Canonical Huffman code length allocator
*
* Based on the algorithm proposed by R. L. Milidiú, A. A. Pessoa and E. S. Laber in "In-place
* Length-Restricted Prefix Coding" (see: http://www-di.inf.puc-rio.br/~laber/public/spire98.ps)
* and incorporating additional ideas from the implementation of "shcodec" by Simakov Alexander
* (see: http://webcenter.ru/~xander/)
*/
public class HuffmanAllocator {
/**
* FIRST() function
* @param array The code length array
* @param i The input position
* @param nodesToMove The number of internal nodes to be relocated
* @return The smallest {@code k} such that {@code nodesToMove <= k <= i} and
* {@code i <= (array[k] % array.length)}
*/
private static int first (final int[] array, int i, final int nodesToMove) {
final int length = array.length;
final int limit = i;
int k = array.length - 2;
while ((i >= nodesToMove) && ((array[i] % length) > limit)) {
k = i;
i -= (limit - i + 1);
}
i = Math.max (nodesToMove - 1, i);
while (k > (i + 1)) {
int temp = (i + k) >> 1;
if ((array[temp] % length) > limit) {
k = temp;
} else {
i = temp;
}
}
return k;
}
/**
* Fills the code array with extended parent pointers
* @param array The code length array
*/
private static void setExtendedParentPointers (final int[] array) {
final int length = array.length;
array[0] += array[1];
for (int headNode = 0, tailNode = 1, topNode = 2; tailNode < (length - 1); tailNode++) {
int temp;
if ((topNode >= length) || (array[headNode] < array[topNode])) {
temp = array[headNode];
array[headNode++] = tailNode;
} else {
temp = array[topNode++];
}
if ((topNode >= length) || ((headNode < tailNode) && (array[headNode] < array[topNode]))) {
temp += array[headNode];
array[headNode++] = tailNode + length;
} else {
temp += array[topNode++];
}
array[tailNode] = temp;
}
}
/**
* Finds the number of nodes to relocate in order to achieve a given code length limit
* @param array The code length array
* @param maximumLength The maximum bit length for the generated codes
* @return The number of nodes to relocate
*/
private static int findNodesToRelocate (final int[] array, final int maximumLength) {
int currentNode = array.length - 2;
for (int currentDepth = 1; (currentDepth < (maximumLength - 1)) && (currentNode > 1); currentDepth++) {
currentNode = first (array, currentNode - 1, 0);
}
return currentNode;
}
/**
* A final allocation pass with no code length limit
* @param array The code length array
*/
private static void allocateNodeLengths (final int[] array) {
int firstNode = array.length - 2;
int nextNode = array.length - 1;
for (int currentDepth = 1, availableNodes = 2; availableNodes > 0; currentDepth++) {
final int lastNode = firstNode;
firstNode = first (array, lastNode - 1, 0);
for (int i = availableNodes - (lastNode - firstNode); i > 0; i--) {
array[nextNode--] = currentDepth;
}
availableNodes = (lastNode - firstNode) << 1;
}
}
/**
* A final allocation pass that relocates nodes in order to achieve a maximum code length limit
* @param array The code length array
* @param nodesToMove The number of internal nodes to be relocated
* @param insertDepth The depth at which to insert relocated nodes
*/
private static void allocateNodeLengthsWithRelocation (final int[] array, final int nodesToMove, final int insertDepth) {
int firstNode = array.length - 2;
int nextNode = array.length - 1;
int currentDepth = (insertDepth == 1) ? 2 : 1;
int nodesLeftToMove = (insertDepth == 1) ? nodesToMove - 2 : nodesToMove;
for (int availableNodes = currentDepth << 1; availableNodes > 0; currentDepth++) {
final int lastNode = firstNode;
firstNode = (firstNode <= nodesToMove) ? firstNode : first (array, lastNode - 1, nodesToMove);
int offset = 0;
if (currentDepth >= insertDepth) {
offset = Math.min (nodesLeftToMove, 1 << (currentDepth - insertDepth));
} else if (currentDepth == (insertDepth - 1)) {
offset = 1;
if ((array[firstNode]) == lastNode) {
firstNode++;
}
}
for (int i = availableNodes - (lastNode - firstNode + offset); i > 0; i--) {
array[nextNode--] = currentDepth;
}
nodesLeftToMove -= offset;
availableNodes = (lastNode - firstNode + offset) << 1;
}
}
/**
* Allocates Canonical Huffman code lengths in place based on a sorted frequency array
* @param array On input, a sorted array of symbol frequencies; On output, an array of Canonical
* Huffman code lengths
* @param maximumLength The maximum code length. Must be at least {@code ceil(log2(array.length))}
*/
public static void allocateHuffmanCodeLengths (final int[] array, final int maximumLength) {
switch (array.length) {
case 2:
array[1] = 1;
case 1:
array[0] = 1;
return;
}
/* Pass 1 : Set extended parent pointers */
setExtendedParentPointers (array);
/* Pass 2 : Find number of nodes to relocate in order to achieve maximum code length */
int nodesToRelocate = findNodesToRelocate (array, maximumLength);
/* Pass 3 : Generate code lengths */
if ((array[0] % array.length) >= nodesToRelocate) {
allocateNodeLengths (array);
} else {
int insertDepth = maximumLength - (32 - Integer.numberOfLeadingZeros (nodesToRelocate - 1));
allocateNodeLengthsWithRelocation (array, nodesToRelocate, insertDepth);
}
}
/**
* Non-instantiable
*/
private HuffmanAllocator() { }
}
| routeKIT/routeKIT | src/org/itadaki/bzip2/HuffmanAllocator.java | Java | gpl-3.0 | 6,762 |
/*
* #%~
* org.overture.ide.core
* %%
* Copyright (C) 2008 - 2014 Overture
* %%
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public
* License along with this program. If not, see
* <http://www.gnu.org/licenses/gpl-3.0.html>.
* #~%
*/
package org.overture.ide.core.resources;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import java.util.Vector;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.eclipse.core.resources.IContainer;
import org.eclipse.core.resources.IFolder;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IPath;
import org.overture.ide.core.VdmCore;
import org.overture.ide.internal.core.ResourceManager;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
public class ModelBuildPath
{
final IVdmProject vdmProject;
final IProject project;
final File modelPathFile;
List<IContainer> srcPaths = new Vector<IContainer>();
IContainer output;
IContainer library;
public ModelBuildPath(IVdmProject project)
{
this.vdmProject = project;
this.project = (IProject) this.vdmProject.getAdapter(IProject.class);
IPath base = this.project.getLocation();
base = base.append(".modelpath");
this.modelPathFile = base.toFile();
this.output = this.project.getFolder("generated");
this.library = this.project.getFolder("lib");
parse();
}
private boolean hasModelPath()
{
return this.modelPathFile.exists();
}
private IContainer getDefaultModelSrcPath()
{
return this.project;
}
public List<IContainer> getModelSrcPaths()
{
List<IContainer> tmp = new Vector<IContainer>(srcPaths.size());
tmp.addAll(srcPaths);
return tmp;
}
public synchronized IContainer getOutput()
{
return this.output;
}
public synchronized IContainer getLibrary()
{
return this.library;
}
private synchronized void parse()
{
if (!hasModelPath())
{
srcPaths.add(getDefaultModelSrcPath());
return;
}
try
{
File file = this.modelPathFile;
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = db.parse(file);
doc.getDocumentElement().normalize();
NodeList nodeLst = doc.getElementsByTagName("modelpathentry");
for (int s = 0; s < nodeLst.getLength(); s++)
{
Node fstNode = nodeLst.item(s);
if (fstNode.getNodeType() == Node.ELEMENT_NODE)
{
Node kindAttribute = fstNode.getAttributes().getNamedItem("kind");
String kindValue = kindAttribute.getNodeValue();
if (kindValue != null)
{
if (kindValue.equals("src"))
{
Node pathAttribute = fstNode.getAttributes().getNamedItem("path");
String pathValue = pathAttribute.getNodeValue();
if(pathValue.equals("."))
{
add(getDefaultModelSrcPath());
}else
{
add(this.project.getFolder(pathValue));
}
} else if (kindValue.equals("output"))
{
Node pathAttribute = fstNode.getAttributes().getNamedItem("path");
String pathValue = pathAttribute.getNodeValue();
output = this.project.getFolder(pathValue);
} else if (kindValue.equals("library"))
{
Node pathAttribute = fstNode.getAttributes().getNamedItem("path");
String pathValue = pathAttribute.getNodeValue();
library = this.project.getFolder(pathValue);
}
}
}
}
if(srcPaths.isEmpty())
{
srcPaths.add(getDefaultModelSrcPath());
}
} catch (Exception e)
{
VdmCore.log("Faild to parse .modelpath file", e);
}
}
public synchronized void setOutput(IContainer container)
{
this.output = container;
}
public synchronized void setLibrary(IContainer container)
{
this.library = container;
}
public synchronized void add(IContainer container)
{
if(container instanceof IProject)
{
srcPaths.clear();
}
else if(container instanceof IFolder)
{
String fullPath = container.getProjectRelativePath().toString();
boolean flag = true;
for (IContainer s : srcPaths)
{
flag = flag && s.getProjectRelativePath().toString().startsWith(fullPath);
}
if(flag)
srcPaths.clear();
}
if (!srcPaths.contains(container))
{
srcPaths.add(container);
}
}
public synchronized void remove(IContainer container)
{
if (srcPaths.contains(container))
{
srcPaths.remove(container);
}
}
public synchronized boolean contains(IContainer container)
{
return srcPaths.contains(container);
}
public synchronized void save() throws CoreException
{
StringBuffer sb = new StringBuffer();
sb.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
sb.append("<modelpath>\n");
for (IContainer src : srcPaths)
{
if (src.getProjectRelativePath().toString().length() > 0)
{
sb.append("\t<modelpathentry kind=\"src\" path=\""
+ src.getProjectRelativePath() + "\"/>\n");
}else if (src instanceof IProject)
{
sb.append("\t<modelpathentry kind=\"src\" path=\".\"/>\n");
}
}
if (output != null
&& output.getProjectRelativePath().toString().length() > 0)
{
sb.append("\t<modelpathentry kind=\"output\" path=\""
+ output.getProjectRelativePath() + "\"/>\n");
}
if (library != null
&& library.getProjectRelativePath().toString().length() > 0)
{
sb.append("\t<modelpathentry kind=\"library\" path=\""
+ library.getProjectRelativePath() + "\"/>\n");
}
sb.append("</modelpath>");
PrintWriter out = null;
try
{
FileWriter outFile = new FileWriter(this.modelPathFile);
out = new PrintWriter(outFile);
out.println(sb.toString());
} catch (IOException e)
{
VdmCore.log("Faild to save .modelpath file", e);
} finally
{
if (out != null)
{
out.close();
}
}
ResourceManager.getInstance().syncBuildPath(vdmProject);
}
/**
* Reload the build path and discard any un-saved changes
*/
public void reload()
{
parse();
}
}
| jmcPereira/overture | ide/core/src/main/java/org/overture/ide/core/resources/ModelBuildPath.java | Java | gpl-3.0 | 6,618 |
/**
* This Source Code Form is subject to the terms of the Mozilla Public License,
* v. 2.0. If a copy of the MPL was not distributed with this file, You can
* obtain one at http://mozilla.org/MPL/2.0/. OpenMRS is also distributed under
* the terms of the Healthcare Disclaimer located at http://openmrs.org/license.
*
* Copyright (C) OpenMRS Inc. OpenMRS is a registered trademark and the OpenMRS
* graphic logo is a trademark of OpenMRS Inc.
*/
package org.openmrs.annotation;
import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
* Place it on classes which you want to be beans created conditionally based on
* OpenMRS version and/or started modules.
*
* @since 1.10, 1.9.8, 1.8.5, 1.7.5
*/
@Target( { ElementType.TYPE })
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface OpenmrsProfile {
public String openmrsVersion() default "";
public String[] modules() default {};
}
| jembi/openmrs-core | api/src/main/java/org/openmrs/annotation/OpenmrsProfile.java | Java | mpl-2.0 | 1,065 |
package tc.oc.commons.core.plugin;
import java.util.Set;
import tc.oc.commons.core.commands.CommandRegistry;
import tc.oc.commons.core.commands.Commands;
import tc.oc.commons.core.commands.NestedCommands;
import tc.oc.minecraft.api.event.Activatable;
import tc.oc.commons.core.inject.Facet;
/**
* Something that needs to be enabled and disabled (along with a plugin).
*
* Each plugin has a private set of facets, configured through a {@link PluginFacetBinder}.
* To get the instances, @Inject a {@link Set< PluginFacet >}.
*
* Facets are automatically enabled and disabled at the same time as the
* plugin they are bound to.
*
* If a facet implements the {@link tc.oc.minecraft.api.event.Listener} interfaces,
* it will also be registered to receive events.
*
* If it implements {@link Commands} or {@link NestedCommands}, it will be registered
* through a {@link CommandRegistry}.
*
* Specific plugins may do other automatic things with their own facets, be we
* don't yet have a framework for extending facets across all plugins.
*/
public interface PluginFacet extends Facet, Activatable {
}
| cswhite2000/ProjectAres | Util/core/src/main/java/tc/oc/commons/core/plugin/PluginFacet.java | Java | agpl-3.0 | 1,116 |
package org.cbioportal.service.impl;
import java.math.BigDecimal;
import java.util.*;
import org.cbioportal.model.*;
import org.cbioportal.model.meta.GenericAssayMeta;
import org.cbioportal.persistence.MolecularDataRepository;
import org.cbioportal.service.GeneService;
import org.cbioportal.service.GenericAssayService;
import org.cbioportal.service.MolecularProfileService;
import org.cbioportal.service.SampleService;
import org.cbioportal.service.exception.MolecularProfileNotFoundException;
import org.cbioportal.service.util.ExpressionEnrichmentUtil;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.Spy;
import org.mockito.junit.MockitoJUnitRunner;
@RunWith(MockitoJUnitRunner.class)
public class ExpressionEnrichmentServiceImplTest extends BaseServiceImplTest {
@InjectMocks
private ExpressionEnrichmentServiceImpl enrichmentServiceImpl;
@Mock
private SampleService sampleService;
@Mock
private MolecularProfileService molecularProfileService;
@Mock
private MolecularDataRepository molecularDataRepository;
@Mock
private GeneService geneService;
@Spy
@InjectMocks
private ExpressionEnrichmentUtil expressionEnrichmentUtil;
@Mock
private GenericAssayService genericAssayService;
CancerStudy cancerStudy = new CancerStudy();
MolecularProfile geneMolecularProfile = new MolecularProfile();
MolecularProfileSamples molecularProfileSamples = new MolecularProfileSamples();
List<Sample> samples = new ArrayList<>();
Map<String, List<MolecularProfileCaseIdentifier>> molecularProfileCaseSets = new HashMap<>();
Map<String, List<MolecularProfileCaseIdentifier>> molecularProfilePatientLevelCaseSets = new HashMap<>();
// patient level only data
public static final String SAMPLE_ID5 = "sample_id5";
@Before
public void setup() throws MolecularProfileNotFoundException {
cancerStudy.setReferenceGenome(ReferenceGenome.HOMO_SAPIENS_DEFAULT_GENOME_NAME);
cancerStudy.setCancerStudyIdentifier(STUDY_ID);
geneMolecularProfile.setCancerStudyIdentifier(STUDY_ID);
geneMolecularProfile.setStableId(MOLECULAR_PROFILE_ID);
geneMolecularProfile.setCancerStudy(cancerStudy);
molecularProfileSamples.setMolecularProfileId(MOLECULAR_PROFILE_ID);
molecularProfileSamples.setCommaSeparatedSampleIds("1,2,3,4");
Sample sample1 = new Sample();
sample1.setStableId(SAMPLE_ID1);
sample1.setInternalId(1);
sample1.setCancerStudyIdentifier(STUDY_ID);
sample1.setPatientId(1);
samples.add(sample1);
Sample sample2 = new Sample();
sample2.setStableId(SAMPLE_ID2);
sample2.setInternalId(2);
sample2.setCancerStudyIdentifier(STUDY_ID);
sample2.setPatientId(2);
samples.add(sample2);
Sample sample3 = new Sample();
sample3.setStableId(SAMPLE_ID3);
sample3.setInternalId(3);
sample3.setCancerStudyIdentifier(STUDY_ID);
sample3.setPatientId(3);
samples.add(sample3);
Sample sample4 = new Sample();
sample4.setStableId(SAMPLE_ID4);
sample4.setInternalId(4);
sample4.setCancerStudyIdentifier(STUDY_ID);
sample4.setPatientId(4);
samples.add(sample4);
List<MolecularProfileCaseIdentifier> alteredSampleIdentifieres = new ArrayList<>();
List<MolecularProfileCaseIdentifier> unalteredSampleIdentifieres = new ArrayList<>();
List<MolecularProfileCaseIdentifier> unalteredPatientLevelSampleIdentifieres = new ArrayList<>();
MolecularProfileCaseIdentifier caseIdentifier1 = new MolecularProfileCaseIdentifier();
caseIdentifier1.setMolecularProfileId(MOLECULAR_PROFILE_ID);
caseIdentifier1.setCaseId(SAMPLE_ID1);
alteredSampleIdentifieres.add(caseIdentifier1);
MolecularProfileCaseIdentifier caseIdentifier2 = new MolecularProfileCaseIdentifier();
caseIdentifier2.setMolecularProfileId(MOLECULAR_PROFILE_ID);
caseIdentifier2.setCaseId(SAMPLE_ID2);
alteredSampleIdentifieres.add(caseIdentifier2);
MolecularProfileCaseIdentifier caseIdentifier3 = new MolecularProfileCaseIdentifier();
caseIdentifier3.setMolecularProfileId(MOLECULAR_PROFILE_ID);
caseIdentifier3.setCaseId(SAMPLE_ID3);
unalteredSampleIdentifieres.add(caseIdentifier3);
unalteredPatientLevelSampleIdentifieres.add(caseIdentifier3);
MolecularProfileCaseIdentifier caseIdentifier4 = new MolecularProfileCaseIdentifier();
caseIdentifier4.setMolecularProfileId(MOLECULAR_PROFILE_ID);
caseIdentifier4.setCaseId(SAMPLE_ID4);
unalteredSampleIdentifieres.add(caseIdentifier4);
unalteredPatientLevelSampleIdentifieres.add(caseIdentifier4);
// patient level only data
MolecularProfileCaseIdentifier caseIdentifier5 = new MolecularProfileCaseIdentifier();
caseIdentifier5.setMolecularProfileId(MOLECULAR_PROFILE_ID);
caseIdentifier5.setCaseId(SAMPLE_ID5);
unalteredPatientLevelSampleIdentifieres.add(caseIdentifier5);
molecularProfileCaseSets.put("altered samples", alteredSampleIdentifieres);
molecularProfileCaseSets.put("unaltered samples", unalteredSampleIdentifieres);
molecularProfilePatientLevelCaseSets.put("altered samples", alteredSampleIdentifieres);
molecularProfilePatientLevelCaseSets.put("unaltered samples", unalteredPatientLevelSampleIdentifieres);
Mockito.when(molecularProfileService.getMolecularProfile(MOLECULAR_PROFILE_ID))
.thenReturn(geneMolecularProfile);
Mockito.when(molecularDataRepository.getCommaSeparatedSampleIdsOfMolecularProfile(MOLECULAR_PROFILE_ID))
.thenReturn(molecularProfileSamples);
Mockito.when(sampleService.fetchSamples(Arrays.asList(STUDY_ID, STUDY_ID, STUDY_ID, STUDY_ID),
Arrays.asList(SAMPLE_ID3, SAMPLE_ID4, SAMPLE_ID1, SAMPLE_ID2), "ID")).thenReturn(samples);
}
@Test
public void getGenomicEnrichments() throws Exception {
geneMolecularProfile.setMolecularAlterationType(MolecularProfile.MolecularAlterationType.MRNA_EXPRESSION);
List<GeneMolecularAlteration> molecularDataList = new ArrayList<GeneMolecularAlteration>();
GeneMolecularAlteration geneMolecularAlteration1 = new GeneMolecularAlteration();
geneMolecularAlteration1.setEntrezGeneId(ENTREZ_GENE_ID_2);
geneMolecularAlteration1.setValues("2,3,2.1,3");
molecularDataList.add(geneMolecularAlteration1);
GeneMolecularAlteration geneMolecularAlteration2 = new GeneMolecularAlteration();
geneMolecularAlteration2.setEntrezGeneId(ENTREZ_GENE_ID_3);
geneMolecularAlteration2.setValues("1.1,5,2.3,3");
molecularDataList.add(geneMolecularAlteration2);
Mockito.when(molecularDataRepository.getGeneMolecularAlterationsIterableFast(MOLECULAR_PROFILE_ID))
.thenReturn(molecularDataList);
List<Gene> expectedGeneList = new ArrayList<>();
Gene gene1 = new Gene();
gene1.setEntrezGeneId(ENTREZ_GENE_ID_2);
gene1.setHugoGeneSymbol(HUGO_GENE_SYMBOL_2);
expectedGeneList.add(gene1);
Gene gene2 = new Gene();
gene2.setEntrezGeneId(ENTREZ_GENE_ID_3);
gene2.setHugoGeneSymbol(HUGO_GENE_SYMBOL_3);
expectedGeneList.add(gene2);
Mockito.when(geneService.fetchGenes(Arrays.asList("2", "3"), "ENTREZ_GENE_ID", "SUMMARY"))
.thenReturn(expectedGeneList);
List<GenomicEnrichment> result = enrichmentServiceImpl.getGenomicEnrichments(MOLECULAR_PROFILE_ID,
molecularProfileCaseSets, EnrichmentType.SAMPLE);
Assert.assertEquals(2, result.size());
GenomicEnrichment expressionEnrichment = result.get(0);
Assert.assertEquals(ENTREZ_GENE_ID_2, expressionEnrichment.getEntrezGeneId());
Assert.assertEquals(HUGO_GENE_SYMBOL_2, expressionEnrichment.getHugoGeneSymbol());
Assert.assertEquals(null, expressionEnrichment.getCytoband());
Assert.assertEquals(2, expressionEnrichment.getGroupsStatistics().size());
GroupStatistics unalteredGroupStats = expressionEnrichment.getGroupsStatistics().get(0);
Assert.assertEquals("unaltered samples", unalteredGroupStats.getName());
Assert.assertEquals(new BigDecimal("2.55"), unalteredGroupStats.getMeanExpression());
Assert.assertEquals(new BigDecimal("0.6363961030678927"), unalteredGroupStats.getStandardDeviation());
GroupStatistics alteredGroupStats = expressionEnrichment.getGroupsStatistics().get(1);
Assert.assertEquals("altered samples", alteredGroupStats.getName());
Assert.assertEquals(new BigDecimal("2.5"), alteredGroupStats.getMeanExpression());
Assert.assertEquals(new BigDecimal("0.7071067811865476"), alteredGroupStats.getStandardDeviation());
Assert.assertEquals(new BigDecimal("0.9475795430163914"), expressionEnrichment.getpValue());
expressionEnrichment = result.get(1);
Assert.assertEquals(ENTREZ_GENE_ID_3, expressionEnrichment.getEntrezGeneId());
Assert.assertEquals(HUGO_GENE_SYMBOL_3, expressionEnrichment.getHugoGeneSymbol());
Assert.assertEquals(null, expressionEnrichment.getCytoband());
Assert.assertEquals(2, expressionEnrichment.getGroupsStatistics().size());
unalteredGroupStats = expressionEnrichment.getGroupsStatistics().get(0);
Assert.assertEquals("unaltered samples", unalteredGroupStats.getName());
Assert.assertEquals(new BigDecimal("2.65"), unalteredGroupStats.getMeanExpression());
Assert.assertEquals(new BigDecimal("0.4949747468305834"), unalteredGroupStats.getStandardDeviation());
alteredGroupStats = expressionEnrichment.getGroupsStatistics().get(1);
Assert.assertEquals("altered samples", alteredGroupStats.getName());
Assert.assertEquals(new BigDecimal("3.05"), alteredGroupStats.getMeanExpression());
Assert.assertEquals(new BigDecimal("2.7577164466275352"), alteredGroupStats.getStandardDeviation());
Assert.assertEquals(new BigDecimal("0.8716148250471419"), expressionEnrichment.getpValue());
}
@Test
public void getGenericAssayEnrichments() throws Exception {
geneMolecularProfile.setMolecularAlterationType(MolecularProfile.MolecularAlterationType.GENERIC_ASSAY);
List<GenericAssayMolecularAlteration> molecularDataList = new ArrayList<GenericAssayMolecularAlteration>();
GenericAssayMolecularAlteration genericAssayMolecularAlteration1 = new GenericAssayMolecularAlteration();
genericAssayMolecularAlteration1.setGenericAssayStableId(HUGO_GENE_SYMBOL_1);
genericAssayMolecularAlteration1.setValues("2,3,2.1,3");
molecularDataList.add(genericAssayMolecularAlteration1);
GenericAssayMolecularAlteration genericAssayMolecularAlteration2 = new GenericAssayMolecularAlteration();
genericAssayMolecularAlteration2.setGenericAssayStableId(HUGO_GENE_SYMBOL_2);
genericAssayMolecularAlteration2.setValues("1.1,5,2.3,3");
molecularDataList.add(genericAssayMolecularAlteration2);
Mockito.when(molecularDataRepository.getGenericAssayMolecularAlterationsIterable(MOLECULAR_PROFILE_ID, null,
"SUMMARY")).thenReturn(molecularDataList);
Mockito.when(genericAssayService.getGenericAssayMetaByStableIdsAndMolecularIds(
Arrays.asList(HUGO_GENE_SYMBOL_1, HUGO_GENE_SYMBOL_2),
Arrays.asList(MOLECULAR_PROFILE_ID, MOLECULAR_PROFILE_ID), "SUMMARY"))
.thenReturn(Arrays.asList(new GenericAssayMeta(HUGO_GENE_SYMBOL_1),
new GenericAssayMeta(HUGO_GENE_SYMBOL_2)));
List<GenericAssayEnrichment> result = enrichmentServiceImpl.getGenericAssayEnrichments(MOLECULAR_PROFILE_ID,
molecularProfileCaseSets, EnrichmentType.SAMPLE);
Assert.assertEquals(2, result.size());
GenericAssayEnrichment genericAssayEnrichment = result.get(0);
Assert.assertEquals(HUGO_GENE_SYMBOL_1, genericAssayEnrichment.getStableId());
Assert.assertEquals(2, genericAssayEnrichment.getGroupsStatistics().size());
GroupStatistics unalteredGroupStats = genericAssayEnrichment.getGroupsStatistics().get(0);
Assert.assertEquals("unaltered samples", unalteredGroupStats.getName());
Assert.assertEquals(new BigDecimal("2.55"), unalteredGroupStats.getMeanExpression());
Assert.assertEquals(new BigDecimal("0.6363961030678927"), unalteredGroupStats.getStandardDeviation());
GroupStatistics alteredGroupStats = genericAssayEnrichment.getGroupsStatistics().get(1);
Assert.assertEquals("altered samples", alteredGroupStats.getName());
Assert.assertEquals(new BigDecimal("2.5"), alteredGroupStats.getMeanExpression());
Assert.assertEquals(new BigDecimal("0.7071067811865476"), alteredGroupStats.getStandardDeviation());
Assert.assertEquals(new BigDecimal("0.9475795430163914"), genericAssayEnrichment.getpValue());
genericAssayEnrichment = result.get(1);
Assert.assertEquals(HUGO_GENE_SYMBOL_2, genericAssayEnrichment.getStableId());
Assert.assertEquals(2, genericAssayEnrichment.getGroupsStatistics().size());
unalteredGroupStats = genericAssayEnrichment.getGroupsStatistics().get(0);
Assert.assertEquals("unaltered samples", unalteredGroupStats.getName());
Assert.assertEquals(new BigDecimal("2.65"), unalteredGroupStats.getMeanExpression());
Assert.assertEquals(new BigDecimal("0.4949747468305834"), unalteredGroupStats.getStandardDeviation());
alteredGroupStats = genericAssayEnrichment.getGroupsStatistics().get(1);
Assert.assertEquals("altered samples", alteredGroupStats.getName());
Assert.assertEquals(new BigDecimal("3.05"), alteredGroupStats.getMeanExpression());
Assert.assertEquals(new BigDecimal("2.7577164466275352"), alteredGroupStats.getStandardDeviation());
Assert.assertEquals(new BigDecimal("0.8716148250471419"), genericAssayEnrichment.getpValue());
}
@Test
public void getGenericAssayPatientLevelEnrichments() throws Exception {
geneMolecularProfile.setMolecularAlterationType(MolecularProfile.MolecularAlterationType.GENERIC_ASSAY);
geneMolecularProfile.setPatientLevel(true);
List<GenericAssayMolecularAlteration> molecularDataList = new ArrayList<GenericAssayMolecularAlteration>();
GenericAssayMolecularAlteration genericAssayMolecularAlteration1 = new GenericAssayMolecularAlteration();
genericAssayMolecularAlteration1.setGenericAssayStableId(HUGO_GENE_SYMBOL_1);
genericAssayMolecularAlteration1.setValues("2,3,2.1,3,3,3");
molecularDataList.add(genericAssayMolecularAlteration1);
GenericAssayMolecularAlteration genericAssayMolecularAlteration2 = new GenericAssayMolecularAlteration();
genericAssayMolecularAlteration2.setGenericAssayStableId(HUGO_GENE_SYMBOL_2);
genericAssayMolecularAlteration2.setValues("1.1,5,2.3,3,3");
molecularDataList.add(genericAssayMolecularAlteration2);
Mockito.when(molecularDataRepository.getGenericAssayMolecularAlterationsIterable(MOLECULAR_PROFILE_ID, null,
"SUMMARY")).thenReturn(molecularDataList);
Mockito.when(genericAssayService.getGenericAssayMetaByStableIdsAndMolecularIds(
Arrays.asList(HUGO_GENE_SYMBOL_1, HUGO_GENE_SYMBOL_2),
Arrays.asList(MOLECULAR_PROFILE_ID, MOLECULAR_PROFILE_ID), "SUMMARY"))
.thenReturn(Arrays.asList(new GenericAssayMeta(HUGO_GENE_SYMBOL_1),
new GenericAssayMeta(HUGO_GENE_SYMBOL_2)));
// add 5th sample which is the second sample of patient 4
Sample sample5 = new Sample();
sample5.setStableId(SAMPLE_ID5);
sample5.setInternalId(5);
sample5.setCancerStudyIdentifier(STUDY_ID);
sample5.setPatientId(4);
samples.add(sample5);
Mockito.when(sampleService.fetchSamples(Arrays.asList(STUDY_ID, STUDY_ID, STUDY_ID, STUDY_ID, STUDY_ID), Arrays.asList(SAMPLE_ID3, SAMPLE_ID4, SAMPLE_ID5, SAMPLE_ID1, SAMPLE_ID2), "ID")).thenReturn(samples);
List<GenericAssayEnrichment> result = enrichmentServiceImpl.getGenericAssayEnrichments(MOLECULAR_PROFILE_ID, molecularProfilePatientLevelCaseSets, EnrichmentType.SAMPLE);
Assert.assertEquals(2, result.size());
GenericAssayEnrichment genericAssayEnrichment = result.get(0);
Assert.assertEquals(HUGO_GENE_SYMBOL_1, genericAssayEnrichment.getStableId());
Assert.assertEquals(2, genericAssayEnrichment.getGroupsStatistics().size());
GroupStatistics unalteredGroupStats = genericAssayEnrichment.getGroupsStatistics().get(0);
Assert.assertEquals("unaltered samples", unalteredGroupStats.getName());
Assert.assertEquals(new BigDecimal("2.55"), unalteredGroupStats.getMeanExpression());
Assert.assertEquals(new BigDecimal("0.6363961030678927"), unalteredGroupStats.getStandardDeviation());
GroupStatistics alteredGroupStats = genericAssayEnrichment.getGroupsStatistics().get(1);
Assert.assertEquals("altered samples", alteredGroupStats.getName());
Assert.assertEquals(new BigDecimal("2.5"), alteredGroupStats.getMeanExpression());
Assert.assertEquals(new BigDecimal("0.7071067811865476"), alteredGroupStats.getStandardDeviation());
Assert.assertEquals(new BigDecimal("0.9475795430163914"), genericAssayEnrichment.getpValue());
genericAssayEnrichment = result.get(1);
Assert.assertEquals(HUGO_GENE_SYMBOL_2, genericAssayEnrichment.getStableId());
Assert.assertEquals(2, genericAssayEnrichment.getGroupsStatistics().size());
unalteredGroupStats = genericAssayEnrichment.getGroupsStatistics().get(0);
Assert.assertEquals("unaltered samples", unalteredGroupStats.getName());
Assert.assertEquals(new BigDecimal("2.65"), unalteredGroupStats.getMeanExpression());
Assert.assertEquals(new BigDecimal("0.4949747468305834"), unalteredGroupStats.getStandardDeviation());
alteredGroupStats = genericAssayEnrichment.getGroupsStatistics().get(1);
Assert.assertEquals("altered samples", alteredGroupStats.getName());
Assert.assertEquals(new BigDecimal("3.05"), alteredGroupStats.getMeanExpression());
Assert.assertEquals(new BigDecimal("2.7577164466275352"), alteredGroupStats.getStandardDeviation());
Assert.assertEquals(new BigDecimal("0.8716148250471419"), genericAssayEnrichment.getpValue());
}
}
| onursumer/cbioportal | service/src/test/java/org/cbioportal/service/impl/ExpressionEnrichmentServiceImplTest.java | Java | agpl-3.0 | 18,869 |
package org.jboss.hal.testsuite.test.configuration.undertow;
import org.apache.commons.lang.RandomStringUtils;
import org.jboss.arquillian.graphene.page.Page;
import org.jboss.arquillian.junit.Arquillian;
import org.jboss.hal.testsuite.category.Shared;
import org.jboss.hal.testsuite.page.config.UndertowServletPage;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;
import org.junit.runner.RunWith;
import org.wildfly.extras.creaper.core.online.operations.Address;
import org.wildfly.extras.creaper.core.online.operations.OperationException;
import java.io.IOException;
import java.util.concurrent.TimeoutException;
@RunWith(Arquillian.class)
@Category(Shared.class)
public class ServletContainerTestCase extends UndertowTestCaseAbstract {
@Page
private UndertowServletPage page;
//identifiers
private static final String ALLOW_NON_STANDARD_WRAPPERS = "allow-non-standard-wrappers";
private static final String DEFAULT_BUFFER_CACHE = "default-buffer-cache";
private static final String DEFAULT_ENCODING = "default-encoding";
private static final String DEFAULT_SESSION_TIMEOUT = "default-session-timeout";
private static final String DIRECTORY_LISTING = "directory-listing";
private static final String DISABLE_CACHING_FOR_SECURED_PAGES = "disable-caching-for-secured-pages";
private static final String EAGER_FILTER_INITIALIZATION = "eager-filter-initialization";
private static final String IGNORE_FLUSH = "ignore-flush";
private static final String STACK_TRACE_ON_ERROR = "stack-trace-on-error";
private static final String USE_LISTENER_ENCODING = "use-listener-encoding";
//values
private static final String STACK_TRACE_ON_ERROR_VALUE = "all";
private static final String SERVLET_CONTAINER = "servlet-container_" + RandomStringUtils.randomAlphanumeric(5);
private static final Address SERVLET_CONTAINER_ADDRESS = UNDERTOW_ADDRESS.and("servlet-container", SERVLET_CONTAINER);
@BeforeClass
public static void setUp() throws InterruptedException, IOException, TimeoutException {
operations.add(SERVLET_CONTAINER_ADDRESS);
}
@Before
public void before() {
page.navigate();
page.selectServletContainer(SERVLET_CONTAINER);
}
@AfterClass
public static void tearDown() throws InterruptedException, IOException, TimeoutException, OperationException {
operations.remove(SERVLET_CONTAINER_ADDRESS);
}
@Test
public void setAllowNonStandardWrappersToTrue() throws Exception {
editCheckboxAndVerify(SERVLET_CONTAINER_ADDRESS, ALLOW_NON_STANDARD_WRAPPERS, true);
}
@Test
public void setAllowNonStandardWrappersToFalse() throws Exception {
editCheckboxAndVerify(SERVLET_CONTAINER_ADDRESS, ALLOW_NON_STANDARD_WRAPPERS, false);
}
@Test
public void editDefaultBufferCache() throws Exception {
editTextAndVerify(SERVLET_CONTAINER_ADDRESS, DEFAULT_BUFFER_CACHE, undertowOps.createBufferCache());
}
@Test
public void editDefaultEncoding() throws Exception {
editTextAndVerify(SERVLET_CONTAINER_ADDRESS, DEFAULT_ENCODING);
}
@Test
public void editDefaultSessionTimeout() throws Exception {
editTextAndVerify(SERVLET_CONTAINER_ADDRESS, DEFAULT_SESSION_TIMEOUT, 42);
}
@Test
public void editDefaultSessionTimeoutInvalid() throws Exception {
verifyIfErrorAppears(DEFAULT_SESSION_TIMEOUT, "54sdfg");
}
@Test
public void setDirectoryListingToTrue() throws Exception {
editCheckboxAndVerify(SERVLET_CONTAINER_ADDRESS, DIRECTORY_LISTING, true);
}
@Test
public void setDirectoryListingToFalse() throws Exception {
editCheckboxAndVerify(SERVLET_CONTAINER_ADDRESS, DIRECTORY_LISTING, false);
}
@Test
public void setDisableCachingForSecuredPagesToTrue() throws Exception {
editCheckboxAndVerify(SERVLET_CONTAINER_ADDRESS, DISABLE_CACHING_FOR_SECURED_PAGES, true);
}
@Test
public void setDisableCachingForSecuredPagesToFalse() throws Exception {
editCheckboxAndVerify(SERVLET_CONTAINER_ADDRESS, DISABLE_CACHING_FOR_SECURED_PAGES, false);
}
@Test
public void setIgnoreFlushToTrue() throws Exception {
editCheckboxAndVerify(SERVLET_CONTAINER_ADDRESS, IGNORE_FLUSH, true);
}
@Test
public void setIgnoreFlushToFalse() throws Exception {
editCheckboxAndVerify(SERVLET_CONTAINER_ADDRESS, IGNORE_FLUSH, false);
}
@Test
public void setEagerFilterInitializationToTrue() throws Exception {
editCheckboxAndVerify(SERVLET_CONTAINER_ADDRESS, EAGER_FILTER_INITIALIZATION, true);
}
@Test
public void setEagerFilterInitializationToFalse() throws Exception {
editCheckboxAndVerify(SERVLET_CONTAINER_ADDRESS, EAGER_FILTER_INITIALIZATION, false);
}
@Test
public void selectStackTraceOnError() throws Exception {
selectOptionAndVerify(SERVLET_CONTAINER_ADDRESS, STACK_TRACE_ON_ERROR, STACK_TRACE_ON_ERROR_VALUE);
}
@Test
public void setUseListenerEncodingToTrue() throws Exception {
editCheckboxAndVerify(SERVLET_CONTAINER_ADDRESS, USE_LISTENER_ENCODING, true);
}
@Test
public void setUseListenerEncodingToFalse() throws Exception {
editCheckboxAndVerify(SERVLET_CONTAINER_ADDRESS, USE_LISTENER_ENCODING, false);
}
}
| hpehl/testsuite | basic/src/test/java/org/jboss/hal/testsuite/test/configuration/undertow/ServletContainerTestCase.java | Java | lgpl-2.1 | 5,479 |
/*
* eXist Open Source Native XML Database
* Copyright (C) 2001-07 The eXist Project
* http://exist-db.org
*
* This program 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
* of the License, or (at your option) any later version.
*
* This program 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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
* $Id$
*/
package org.exist.management;
import org.exist.management.impl.PerInstanceMBean;
import org.exist.storage.BrokerPool;
import org.exist.util.DatabaseConfigurationException;
/**
* A dummy agent which will be used if JMX is disabled. It just acts as an empty
* placeholder.
*/
public class DummyAgent implements Agent {
@Override
public void initDBInstance(final BrokerPool instance) {
// do nothing
}
@Override
public void closeDBInstance(final BrokerPool instance) {
// nothing to do
}
@Override
public void addMBean(final PerInstanceMBean mbean) throws DatabaseConfigurationException {
// just do nothing
}
@Override
public void changeStatus(final BrokerPool instance, final TaskStatus actualStatus) {
// nothing to do
}
@Override
public void updateStatus(final BrokerPool instance, final int percentage) {
// nothing to do
}
}
| ljo/exist | src/org/exist/management/DummyAgent.java | Java | lgpl-2.1 | 1,815 |
/*
* #%L
* Alfresco Repository
* %%
* Copyright (C) 2005 - 2016 Alfresco Software Limited
* %%
* This file is part of the Alfresco software.
* If the software was purchased under a paid Alfresco license, the terms of
* the paid license agreement will prevail. Otherwise, the software is
* provided under the following open source license terms:
*
* Alfresco 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 3 of the License, or
* (at your option) any later version.
*
* Alfresco 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 Alfresco. If not, see <http://www.gnu.org/licenses/>.
* #L%
*/
package org.alfresco.repo.management.subsystems;
import java.io.IOException;
/**
* @author Andy
*
*/
public class LuceneChildApplicationContextFactory extends ChildApplicationContextFactory
{
/* (non-Javadoc)
* @see org.alfresco.repo.management.subsystems.ChildApplicationContextFactory#createInitialState()
*/
@Override
protected PropertyBackedBeanState createInitialState() throws IOException
{
return new ApplicationContextState(true);
}
protected void destroy(boolean isPermanent)
{
super.destroy(isPermanent);
doInit();
}
}
| Alfresco/alfresco-repository | src/main/java/org/alfresco/repo/management/subsystems/LuceneChildApplicationContextFactory.java | Java | lgpl-3.0 | 1,669 |
/*
* #%L
* Alfresco Repository
* %%
* Copyright (C) 2005 - 2016 Alfresco Software Limited
* %%
* This file is part of the Alfresco software.
* If the software was purchased under a paid Alfresco license, the terms of
* the paid license agreement will prevail. Otherwise, the software is
* provided under the following open source license terms:
*
* Alfresco 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 3 of the License, or
* (at your option) any later version.
*
* Alfresco 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 Alfresco. If not, see <http://www.gnu.org/licenses/>.
* #L%
*/
package org.alfresco.repo.template;
import java.io.StringReader;
import org.alfresco.error.AlfrescoRuntimeException;
import org.alfresco.model.ContentModel;
import org.alfresco.service.ServiceRegistry;
import org.alfresco.service.cmr.repository.ContentReader;
import org.alfresco.service.cmr.repository.NodeRef;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
/**
* Provides functionality to execute a Lucene search string and return TemplateNode objects.
*
* @author Kevin Roast
*/
public class LuceneSearchResultsMap extends BaseSearchResultsMap
{
/**
* Constructor
*
* @param parent The parent TemplateNode to execute searches from
* @param services The ServiceRegistry to use
*/
public LuceneSearchResultsMap(TemplateNode parent, ServiceRegistry services)
{
super(parent, services);
}
/**
* @see org.alfresco.repo.template.BaseTemplateMap#get(java.lang.Object)
*/
public Object get(Object key)
{
// execute the search
return query(key.toString());
}
}
| Alfresco/alfresco-repository | src/main/java/org/alfresco/repo/template/LuceneSearchResultsMap.java | Java | lgpl-3.0 | 2,177 |
/**
* JBoss, Home of Professional Open Source.
* Copyright 2014 Red Hat, Inc., and individual contributors
* as indicated by the @author tags.
*
* 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.jboss.pnc.common.util;
import org.junit.Test;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Stream;
import static org.assertj.core.api.Assertions.assertThat;
/**
* Author: Michal Szynkiewicz, michal.l.szynkiewicz@gmail.com
* Date: 9/15/16
* Time: 1:37 PM
*/
public class StreamCollectorsTest {
@Test
public void shouldFlattenTwoLists() {
List<String> listOne = Arrays.asList("one-1", "one-2", "one-3");
List<String> listTwo = Arrays.asList("two-1", "two-2");
List<String> actual = Stream.of(listOne, listTwo).collect(StreamCollectors.toFlatList());
List<String> expected = new ArrayList<>(listOne);
expected.addAll(listTwo);
assertThat(actual).hasSameElementsAs(expected);
}
@Test
public void shouldFlattenOneList() {
List<String> listOne = Arrays.asList("one-1", "one-2", "one-3");
List<String> actual = Stream.of(listOne).collect(StreamCollectors.toFlatList());
assertThat(actual).hasSameElementsAs(listOne);
}
@Test
public void shouldFlattenNoList() {
List<String> actual = Stream.<List<String>>of().collect(StreamCollectors.toFlatList());
assertThat(actual).isNotNull().isEmpty();
}
} | ruhan1/pnc | common/src/test/java/org/jboss/pnc/common/util/StreamCollectorsTest.java | Java | apache-2.0 | 1,999 |
/**
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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.apache.camel.itest.karaf;
import org.junit.Test;
import org.junit.Ignore;
import org.junit.runner.RunWith;
import org.ops4j.pax.exam.junit.PaxExam;
@Ignore
@RunWith(PaxExam.class)
public class CamelChronicleTest extends BaseKarafTest {
public static final String COMPONENT = "chronicle";
@Test
public void test() throws Exception {
testComponent(COMPONENT);
}
}
| jarst/camel | tests/camel-itest-karaf/src/test/java/org/apache/camel/itest/karaf/CamelChronicleTest.java | Java | apache-2.0 | 1,206 |
/*
* Copyright (c) 2014, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
*
* WSO2 Inc. licenses this file to you 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.wso2.carbon.identity.mgt.util;
import org.apache.axiom.om.util.Base64;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.neethi.Policy;
import org.apache.neethi.PolicyEngine;
import org.wso2.carbon.CarbonConstants;
import org.wso2.carbon.context.PrivilegedCarbonContext;
import org.wso2.carbon.identity.base.IdentityException;
import org.wso2.carbon.identity.mgt.IdentityMgtConfig;
import org.wso2.carbon.identity.mgt.constants.IdentityMgtConstants;
import org.wso2.carbon.identity.mgt.dto.UserDTO;
import org.wso2.carbon.identity.mgt.internal.IdentityMgtServiceComponent;
import org.wso2.carbon.registry.core.RegistryConstants;
import org.wso2.carbon.registry.core.Resource;
import org.wso2.carbon.registry.core.exceptions.RegistryException;
import org.wso2.carbon.registry.core.session.UserRegistry;
import org.wso2.carbon.user.api.Tenant;
import org.wso2.carbon.user.api.UserStoreException;
import org.wso2.carbon.user.api.UserStoreManager;
import org.wso2.carbon.user.core.UserCoreConstants;
import org.wso2.carbon.user.core.service.RealmService;
import org.wso2.carbon.user.core.tenant.TenantManager;
import org.wso2.carbon.user.core.util.UserCoreUtil;
import org.wso2.carbon.utils.multitenancy.MultitenantConstants;
import org.wso2.carbon.utils.multitenancy.MultitenantUtils;
import java.io.ByteArrayInputStream;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.HashMap;
import java.util.Map;
/**
*
*/
public class Utils {
private static final Log log = LogFactory.getLog(Utils.class);
private Utils() {
}
public static UserDTO processUserId(String userId) throws IdentityException {
if (userId == null || userId.trim().length() < 1) {
throw IdentityException.error("Can not proceed with out a user id");
}
UserDTO userDTO = new UserDTO(userId);
if (!IdentityMgtConfig.getInstance().isSaasEnabled()) {
validateTenant(userDTO);
}
userDTO.setTenantId(getTenantId(userDTO.getTenantDomain()));
return userDTO;
}
public static void validateTenant(UserDTO user) throws IdentityException {
if (user.getTenantDomain() != null && !user.getTenantDomain().isEmpty()) {
if (!user.getTenantDomain().equals(
PrivilegedCarbonContext.getThreadLocalCarbonContext()
.getTenantDomain())) {
throw IdentityException.error(
"Failed access to unauthorized tenant domain");
}
user.setTenantId(getTenantId(user.getTenantDomain()));
}
}
/**
* gets no of verified user challenges
*
* @param userDTO bean class that contains user and tenant Information
* @return no of verified challenges
* @throws IdentityException if fails
*/
public static int getVerifiedChallenges(UserDTO userDTO) throws IdentityException {
int noOfChallenges = 0;
try {
UserRegistry registry = IdentityMgtServiceComponent.getRegistryService().
getConfigSystemRegistry(MultitenantConstants.SUPER_TENANT_ID);
String identityKeyMgtPath = IdentityMgtConstants.IDENTITY_MANAGEMENT_CHALLENGES +
RegistryConstants.PATH_SEPARATOR + userDTO.getUserId() +
RegistryConstants.PATH_SEPARATOR + userDTO.getUserId();
Resource resource;
if (registry.resourceExists(identityKeyMgtPath)) {
resource = registry.get(identityKeyMgtPath);
String property = resource.getProperty(IdentityMgtConstants.VERIFIED_CHALLENGES);
if (property != null) {
return Integer.parseInt(property);
}
}
} catch (RegistryException e) {
log.error("Error while processing userKey", e);
}
return noOfChallenges;
}
/**
* gets the tenant id from the tenant domain
*
* @param domain - tenant domain name
* @return tenantId
* @throws IdentityException if fails or tenant doesn't exist
*/
public static int getTenantId(String domain) throws IdentityException {
int tenantId;
TenantManager tenantManager = IdentityMgtServiceComponent.getRealmService().getTenantManager();
if (MultitenantConstants.SUPER_TENANT_DOMAIN_NAME.equals(domain)) {
tenantId = MultitenantConstants.SUPER_TENANT_ID;
if (log.isDebugEnabled()) {
String msg = "Domain is not defined implicitly. So it is Super Tenant domain.";
log.debug(msg);
}
} else {
try {
tenantId = tenantManager.getTenantId(domain);
if (tenantId < 1 && tenantId != MultitenantConstants.SUPER_TENANT_ID) {
String msg = "This action can not be performed by the users in non-existing domains.";
log.error(msg);
throw IdentityException.error(msg);
}
} catch (org.wso2.carbon.user.api.UserStoreException e) {
String msg = "Error in retrieving tenant id of tenant domain: " + domain + ".";
log.error(msg, e);
throw IdentityException.error(msg, e);
}
}
return tenantId;
}
/**
* Get the claims from the user store manager
*
* @param userName user name
* @param tenantId tenantId
* @param claim claim name
* @return claim value
* @throws IdentityException if fails
*/
public static String getClaimFromUserStoreManager(String userName, int tenantId, String claim)
throws IdentityException {
org.wso2.carbon.user.core.UserStoreManager userStoreManager = null;
RealmService realmService = IdentityMgtServiceComponent.getRealmService();
String claimValue = "";
try {
if (realmService.getTenantUserRealm(tenantId) != null) {
userStoreManager = (org.wso2.carbon.user.core.UserStoreManager) realmService.getTenantUserRealm(tenantId).
getUserStoreManager();
}
} catch (Exception e) {
String msg = "Error retrieving the user store manager for tenant id : " + tenantId;
log.error(msg, e);
throw IdentityException.error(msg, e);
}
try {
if (userStoreManager != null) {
Map<String, String> claimsMap = userStoreManager
.getUserClaimValues(userName, new String[]{claim}, UserCoreConstants.DEFAULT_PROFILE);
if (claimsMap != null && !claimsMap.isEmpty()) {
claimValue = claimsMap.get(claim);
}
}
return claimValue;
} catch (Exception e) {
String msg = "Unable to retrieve the claim for user : " + userName;
log.error(msg, e);
throw IdentityException.error(msg, e);
}
}
public static Map<String,String> getClaimsFromUserStoreManager(String userName, int tenantId, String[] claims)
throws IdentityException {
Map<String, String> claimValues = new HashMap<>();
org.wso2.carbon.user.core.UserStoreManager userStoreManager = null;
RealmService realmService = IdentityMgtServiceComponent.getRealmService();
try {
if (realmService.getTenantUserRealm(tenantId) != null) {
userStoreManager = (org.wso2.carbon.user.core.UserStoreManager) realmService.getTenantUserRealm(tenantId).
getUserStoreManager();
}
} catch (UserStoreException e) {
throw IdentityException.error("Error retrieving the user store manager for tenant id : " + tenantId, e);
}
try {
if (userStoreManager != null) {
claimValues = userStoreManager.getUserClaimValues(userName, claims, UserCoreConstants.DEFAULT_PROFILE);
}
} catch (Exception e) {
throw IdentityException.error("Unable to retrieve the claim for user : " + userName, e);
}
return claimValues;
}
/**
* get email address from user store
*
* @param userName user name
* @param tenantId tenant id
* @return email address
*/
public static String getEmailAddressForUser(String userName, int tenantId) {
String email = null;
try {
if (log.isDebugEnabled()) {
log.debug("Retrieving email address from user profile.");
}
Tenant tenant = IdentityMgtServiceComponent.getRealmService().
getTenantManager().getTenant(tenantId);
if (tenant != null && tenant.getAdminName().equals(userName)) {
email = tenant.getEmail();
}
if (email == null || email.trim().length() < 1) {
email = getClaimFromUserStoreManager(userName, tenantId,
UserCoreConstants.ClaimTypeURIs.EMAIL_ADDRESS);
}
if ((email == null || email.trim().length() < 1) && MultitenantUtils.isEmailUserName()) {
email = UserCoreUtil.removeDomainFromName(userName);
}
} catch (Exception e) {
String msg = "Unable to retrieve an email address associated with the given user : " + userName;
log.warn(msg, e); // It is common to have users with no email address defined.
}
return email;
}
/**
* Update Password with the user input
*
* @return true - if password was successfully reset
* @throws IdentityException
*/
public static boolean updatePassword(String userId, int tenantId, String password) throws IdentityException {
String tenantDomain = null;
if (userId == null || userId.trim().length() < 1 ||
password == null || password.trim().length() < 1) {
String msg = "Unable to find the required information for updating password";
log.error(msg);
throw IdentityException.error(msg);
}
try {
UserStoreManager userStoreManager = IdentityMgtServiceComponent.
getRealmService().getTenantUserRealm(tenantId).getUserStoreManager();
userStoreManager.updateCredentialByAdmin(userId, password);
if (log.isDebugEnabled()) {
String msg = "Password is updated for user: " + userId;
log.debug(msg);
}
return true;
} catch (UserStoreException e) {
String msg = "Error in changing the password, user name: " + userId + " domain: " +
tenantDomain + ".";
log.error(msg, e);
throw IdentityException.error(msg, e);
}
}
/**
* @param value
* @return
* @throws UserStoreException
*/
public static String doHash(String value) throws UserStoreException {
try {
String digsestFunction = "SHA-256";
MessageDigest dgst = MessageDigest.getInstance(digsestFunction);
byte[] byteValue = dgst.digest(value.getBytes());
return Base64.encode(byteValue);
} catch (NoSuchAlgorithmException e) {
log.error(e.getMessage(), e);
throw new UserStoreException(e.getMessage(), e);
}
}
/**
* Set claim to user store manager
*
* @param userName user name
* @param tenantId tenant id
* @param claim claim uri
* @param value claim value
* @throws IdentityException if fails
*/
public static void setClaimInUserStoreManager(String userName, int tenantId, String claim,
String value) throws IdentityException {
org.wso2.carbon.user.core.UserStoreManager userStoreManager = null;
RealmService realmService = IdentityMgtServiceComponent.getRealmService();
try {
if (realmService.getTenantUserRealm(tenantId) != null) {
userStoreManager = (org.wso2.carbon.user.core.UserStoreManager) realmService.getTenantUserRealm(tenantId).
getUserStoreManager();
}
} catch (Exception e) {
String msg = "Error retrieving the user store manager for the tenant";
log.error(msg, e);
throw IdentityException.error(msg, e);
}
try {
if (userStoreManager != null) {
String oldValue = userStoreManager.getUserClaimValue(userName, claim, null);
if (oldValue == null || !oldValue.equals(value)) {
Map<String,String> claimMap = new HashMap<String,String>();
claimMap.put(claim, value);
userStoreManager.setUserClaimValues(userName, claimMap, UserCoreConstants.DEFAULT_PROFILE);
}
}
} catch (Exception e) {
String msg = "Unable to set the claim for user : " + userName;
log.error(msg, e);
throw IdentityException.error(msg, e);
}
}
public static String getUserStoreDomainName(String userName) {
int index;
String userDomain;
if ((index = userName.indexOf(CarbonConstants.DOMAIN_SEPARATOR)) >= 0) {
// remove domain name if exist
userDomain = userName.substring(0, index);
} else {
userDomain = UserCoreConstants.PRIMARY_DEFAULT_DOMAIN_NAME;
}
return userDomain;
}
public static String[] getChallengeUris() {
//TODO
return new String[]{IdentityMgtConstants.DEFAULT_CHALLENGE_QUESTION_URI01,
IdentityMgtConstants.DEFAULT_CHALLENGE_QUESTION_URI02};
}
public static Policy getSecurityPolicy() {
String policyString = " <wsp:Policy wsu:Id=\"UTOverTransport\" xmlns:wsp=\"http://schemas.xmlsoap.org/ws/2004/09/policy\"\n" +
" xmlns:wsu=\"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd\">\n" +
" <wsp:ExactlyOne>\n" +
" <wsp:All>\n" +
" <sp:TransportBinding xmlns:sp=\"http://schemas.xmlsoap.org/ws/2005/07/securitypolicy\">\n" +
" <wsp:Policy>\n" +
" <sp:TransportToken>\n" +
" <wsp:Policy>\n" +
" <sp:HttpsToken RequireClientCertificate=\"true\"/>\n" +
" </wsp:Policy>\n" +
" </sp:TransportToken>\n" +
" <sp:AlgorithmSuite>\n" +
" <wsp:Policy>\n" +
" <sp:Basic256/>\n" +
" </wsp:Policy>\n" +
" </sp:AlgorithmSuite>\n" +
" <sp:Layout>\n" +
" <wsp:Policy>\n" +
" <sp:Lax/>\n" +
" </wsp:Policy>\n" +
" </sp:Layout>\n" +
" <sp:IncludeTimestamp/>\n" +
" </wsp:Policy>\n" +
" </sp:TransportBinding>\n" +
" </wsp:All>\n" +
" </wsp:ExactlyOne>\n" +
" </wsp:Policy>";
return PolicyEngine.getPolicy(new ByteArrayInputStream(policyString.getBytes()));
}
}
| wso2/carbon-identity-framework | components/identity-mgt/org.wso2.carbon.identity.mgt/src/main/java/org/wso2/carbon/identity/mgt/util/Utils.java | Java | apache-2.0 | 16,553 |
/**
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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.apache.camel.component.twitter.search;
import org.apache.camel.Consumer;
import org.apache.camel.Processor;
import org.apache.camel.Producer;
import org.apache.camel.component.twitter.AbstractTwitterEndpoint;
import org.apache.camel.component.twitter.TwitterConfiguration;
import org.apache.camel.component.twitter.TwitterHelper;
import org.apache.camel.spi.Metadata;
import org.apache.camel.spi.UriEndpoint;
import org.apache.camel.spi.UriPath;
import org.apache.camel.util.ObjectHelper;
/**
* The Twitter Search component consumes search results.
*/
@UriEndpoint(firstVersion = "2.10.0", scheme = "twitter-search", title = "Twitter Search", syntax = "twitter-search:keywords",
consumerClass = SearchConsumerHandler.class, label = "api,social")
public class TwitterSearchEndpoint extends AbstractTwitterEndpoint {
@UriPath(description = "The search keywords. Multiple values can be separated with comma.")
@Metadata(required = "true")
private String keywords;
public TwitterSearchEndpoint(String uri, String remaining, TwitterSearchComponent component, TwitterConfiguration properties) {
super(uri, component, properties);
this.keywords = remaining;
}
@Override
public Producer createProducer() throws Exception {
return new SearchProducer(this, keywords);
}
@Override
public Consumer createConsumer(Processor processor) throws Exception {
return TwitterHelper.createConsumer(processor, this, new SearchConsumerHandler(this, keywords));
}
}
| yuruki/camel | components/camel-twitter/src/main/java/org/apache/camel/component/twitter/search/TwitterSearchEndpoint.java | Java | apache-2.0 | 2,347 |
/*
* Copyright 2018 Red Hat, Inc. and/or its affiliates.
*
* 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.kie.workbench.common.stunner.bpmn.definition;
import java.util.Objects;
import javax.validation.Valid;
import org.jboss.errai.common.client.api.annotations.MapsTo;
import org.jboss.errai.common.client.api.annotations.Portable;
import org.jboss.errai.databinding.client.api.Bindable;
import org.kie.workbench.common.forms.adf.definitions.annotations.FieldParam;
import org.kie.workbench.common.forms.adf.definitions.annotations.FormDefinition;
import org.kie.workbench.common.forms.adf.definitions.annotations.FormField;
import org.kie.workbench.common.forms.adf.definitions.settings.FieldPolicy;
import org.kie.workbench.common.stunner.bpmn.definition.property.background.BackgroundSet;
import org.kie.workbench.common.stunner.bpmn.definition.property.dimensions.CircleDimensionSet;
import org.kie.workbench.common.stunner.bpmn.definition.property.dimensions.Radius;
import org.kie.workbench.common.stunner.bpmn.definition.property.event.compensation.CompensationEventExecutionSet;
import org.kie.workbench.common.stunner.bpmn.definition.property.font.FontSet;
import org.kie.workbench.common.stunner.bpmn.definition.property.general.BPMNGeneralSet;
import org.kie.workbench.common.stunner.core.definition.annotation.Definition;
import org.kie.workbench.common.stunner.core.definition.annotation.Property;
import org.kie.workbench.common.stunner.core.definition.annotation.morph.Morph;
import org.kie.workbench.common.stunner.core.util.HashUtil;
import static org.kie.workbench.common.forms.adf.engine.shared.formGeneration.processing.fields.fieldInitializers.nestedForms.AbstractEmbeddedFormsInitializer.COLLAPSIBLE_CONTAINER;
import static org.kie.workbench.common.forms.adf.engine.shared.formGeneration.processing.fields.fieldInitializers.nestedForms.AbstractEmbeddedFormsInitializer.FIELD_CONTAINER_PARAM;
@Portable
@Bindable
@Definition
@Morph(base = BaseEndEvent.class)
@FormDefinition(
startElement = "general",
policy = FieldPolicy.ONLY_MARKED,
defaultFieldSettings = {@FieldParam(name = FIELD_CONTAINER_PARAM, value = COLLAPSIBLE_CONTAINER)}
)
public class EndCompensationEvent extends BaseEndEvent {
@Property
@FormField(afterElement = "general")
@Valid
private CompensationEventExecutionSet executionSet;
public EndCompensationEvent() {
this(new BPMNGeneralSet(""),
new BackgroundSet(),
new FontSet(),
new CircleDimensionSet(new Radius()),
new CompensationEventExecutionSet());
}
public EndCompensationEvent(final @MapsTo("general") BPMNGeneralSet general,
final @MapsTo("backgroundSet") BackgroundSet backgroundSet,
final @MapsTo("fontSet") FontSet fontSet,
final @MapsTo("dimensionsSet") CircleDimensionSet dimensionsSet,
final @MapsTo("executionSet") CompensationEventExecutionSet executionSet) {
super(general,
backgroundSet,
fontSet,
dimensionsSet);
this.executionSet = executionSet;
}
public CompensationEventExecutionSet getExecutionSet() {
return executionSet;
}
public void setExecutionSet(CompensationEventExecutionSet executionSet) {
this.executionSet = executionSet;
}
@Override
public int hashCode() {
return HashUtil.combineHashCodes(super.hashCode(),
Objects.hashCode(executionSet));
}
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o instanceof EndCompensationEvent) {
EndCompensationEvent other = (EndCompensationEvent) o;
return super.equals(other) &&
Objects.equals(executionSet,
other.executionSet);
}
return false;
}
}
| jomarko/kie-wb-common | kie-wb-common-stunner/kie-wb-common-stunner-sets/kie-wb-common-stunner-bpmn/kie-wb-common-stunner-bpmn-api/src/main/java/org/kie/workbench/common/stunner/bpmn/definition/EndCompensationEvent.java | Java | apache-2.0 | 4,567 |
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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.apache.zeppelin.rest;
import static org.junit.Assert.assertEquals;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.commons.io.FileUtils;
import org.apache.zeppelin.interpreter.InterpreterSetting;
import org.apache.zeppelin.notebook.Note;
import org.apache.zeppelin.notebook.Paragraph;
import org.apache.zeppelin.scheduler.Job.Status;
import org.apache.zeppelin.server.ZeppelinServer;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import com.google.gson.Gson;
/**
* Test against spark cluster.
* Spark cluster is started by CI server using testing/startSparkCluster.sh
*/
public class ZeppelinSparkClusterTest extends AbstractTestRestApi {
Gson gson = new Gson();
@BeforeClass
public static void init() throws Exception {
AbstractTestRestApi.startUp();
}
@AfterClass
public static void destroy() throws Exception {
AbstractTestRestApi.shutDown();
}
private void waitForFinish(Paragraph p) {
while (p.getStatus() != Status.FINISHED
&& p.getStatus() != Status.ERROR
&& p.getStatus() != Status.ABORT) {
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
@Test
public void basicRDDTransformationAndActionTest() throws IOException {
// create new note
Note note = ZeppelinServer.notebook.createNote();
// run markdown paragraph, again
Paragraph p = note.addParagraph();
Map config = p.getConfig();
config.put("enabled", true);
p.setConfig(config);
p.setText("%spark print(sc.parallelize(1 to 10).reduce(_ + _))");
note.run(p.getId());
waitForFinish(p);
assertEquals(Status.FINISHED, p.getStatus());
assertEquals("55", p.getResult().message());
ZeppelinServer.notebook.removeNote(note.id());
}
@Test
public void pySparkTest() throws IOException {
// create new note
Note note = ZeppelinServer.notebook.createNote();
int sparkVersion = getSparkVersionNumber(note);
if (isPyspark() && sparkVersion >= 12) { // pyspark supported from 1.2.1
// run markdown paragraph, again
Paragraph p = note.addParagraph();
Map config = p.getConfig();
config.put("enabled", true);
p.setConfig(config);
p.setText("%pyspark print(sc.parallelize(range(1, 11)).reduce(lambda a, b: a + b))");
note.run(p.getId());
waitForFinish(p);
assertEquals(Status.FINISHED, p.getStatus());
assertEquals("55\n", p.getResult().message());
}
ZeppelinServer.notebook.removeNote(note.id());
}
@Test
public void pySparkAutoConvertOptionTest() throws IOException {
// create new note
Note note = ZeppelinServer.notebook.createNote();
int sparkVersion = getSparkVersionNumber(note);
if (isPyspark() && sparkVersion >= 14) { // auto_convert enabled from spark 1.4
// run markdown paragraph, again
Paragraph p = note.addParagraph();
Map config = p.getConfig();
config.put("enabled", true);
p.setConfig(config);
p.setText("%pyspark\nfrom pyspark.sql.functions import *\n"
+ "print(sqlContext.range(0, 10).withColumn('uniform', rand(seed=10) * 3.14).count())");
note.run(p.getId());
waitForFinish(p);
assertEquals(Status.FINISHED, p.getStatus());
assertEquals("10\n", p.getResult().message());
}
ZeppelinServer.notebook.removeNote(note.id());
}
@Test
public void zRunTest() throws IOException {
// create new note
Note note = ZeppelinServer.notebook.createNote();
Paragraph p0 = note.addParagraph();
Map config0 = p0.getConfig();
config0.put("enabled", true);
p0.setConfig(config0);
p0.setText("%spark z.run(1)");
Paragraph p1 = note.addParagraph();
Map config1 = p1.getConfig();
config1.put("enabled", true);
p1.setConfig(config1);
p1.setText("%spark val a=10");
Paragraph p2 = note.addParagraph();
Map config2 = p2.getConfig();
config2.put("enabled", true);
p2.setConfig(config2);
p2.setText("%spark print(a)");
note.run(p0.getId());
waitForFinish(p0);
assertEquals(Status.FINISHED, p0.getStatus());
note.run(p2.getId());
waitForFinish(p2);
assertEquals(Status.FINISHED, p2.getStatus());
assertEquals("10", p2.getResult().message());
ZeppelinServer.notebook.removeNote(note.id());
}
@Test
public void pySparkDepLoaderTest() throws IOException {
// create new note
Note note = ZeppelinServer.notebook.createNote();
if (isPyspark() && getSparkVersionNumber(note) >= 14) {
// restart spark interpreter
List<InterpreterSetting> settings =
ZeppelinServer.notebook.getBindedInterpreterSettings(note.id());
for (InterpreterSetting setting : settings) {
if (setting.getGroup().equals("spark")) {
ZeppelinServer.notebook.getInterpreterFactory().restart(setting.id());
break;
}
}
// load dep
Paragraph p0 = note.addParagraph();
Map config = p0.getConfig();
config.put("enabled", true);
p0.setConfig(config);
p0.setText("%dep z.load(\"com.databricks:spark-csv_2.11:1.2.0\")");
note.run(p0.getId());
waitForFinish(p0);
assertEquals(Status.FINISHED, p0.getStatus());
// write test csv file
File tmpFile = File.createTempFile("test", "csv");
FileUtils.write(tmpFile, "a,b\n1,2");
// load data using libraries from dep loader
Paragraph p1 = note.addParagraph();
p1.setConfig(config);
p1.setText("%pyspark\n" +
"from pyspark.sql import SQLContext\n" +
"print(sqlContext.read.format('com.databricks.spark.csv')" +
".load('"+ tmpFile.getAbsolutePath() +"').count())");
note.run(p1.getId());
waitForFinish(p1);
assertEquals(Status.FINISHED, p1.getStatus());
assertEquals("2\n", p1.getResult().message());
}
}
/**
* Get spark version number as a numerical value.
* eg. 1.1.x => 11, 1.2.x => 12, 1.3.x => 13 ...
*/
private int getSparkVersionNumber(Note note) {
Paragraph p = note.addParagraph();
Map config = p.getConfig();
config.put("enabled", true);
p.setConfig(config);
p.setText("%spark print(sc.version)");
note.run(p.getId());
waitForFinish(p);
assertEquals(Status.FINISHED, p.getStatus());
String sparkVersion = p.getResult().message();
System.out.println("Spark version detected " + sparkVersion);
String[] split = sparkVersion.split("\\.");
int version = Integer.parseInt(split[0]) * 10 + Integer.parseInt(split[1]);
return version;
}
}
| issaclee/silkroad | zeppelin-server/src/testpjava/org/apache/zeppelin/rest/ZeppelinSparkClusterTest.java | Java | apache-2.0 | 7,524 |
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/
package org.elasticsearch.xpack.watcher.notification.email;
import org.elasticsearch.common.Nullable;
import org.elasticsearch.common.settings.ClusterSettings;
import org.elasticsearch.common.settings.SecureSetting;
import org.elasticsearch.common.settings.SecureString;
import org.elasticsearch.common.settings.Setting;
import org.elasticsearch.common.settings.Setting.Property;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.unit.TimeValue;
import org.elasticsearch.xpack.core.watcher.crypto.CryptoService;
import org.elasticsearch.xpack.watcher.notification.NotificationService;
import javax.mail.MessagingException;
import java.util.Arrays;
import java.util.List;
/**
* A component to store email credentials and handle sending email notifications.
*/
public class EmailService extends NotificationService<Account> {
private static final Setting<String> SETTING_DEFAULT_ACCOUNT =
Setting.simpleString("xpack.notification.email.default_account", Property.Dynamic, Property.NodeScope);
private static final Setting.AffixSetting<String> SETTING_PROFILE =
Setting.affixKeySetting("xpack.notification.email.account.", "profile",
(key) -> Setting.simpleString(key, Property.Dynamic, Property.NodeScope));
private static final Setting.AffixSetting<Settings> SETTING_EMAIL_DEFAULTS =
Setting.affixKeySetting("xpack.notification.email.account.", "email_defaults",
(key) -> Setting.groupSetting(key + ".", Property.Dynamic, Property.NodeScope));
// settings that can be configured as smtp properties
private static final Setting.AffixSetting<Boolean> SETTING_SMTP_AUTH =
Setting.affixKeySetting("xpack.notification.email.account.", "smtp.auth",
(key) -> Setting.boolSetting(key, false, Property.Dynamic, Property.NodeScope));
private static final Setting.AffixSetting<Boolean> SETTING_SMTP_STARTTLS_ENABLE =
Setting.affixKeySetting("xpack.notification.email.account.", "smtp.starttls.enable",
(key) -> Setting.boolSetting(key, false, Property.Dynamic, Property.NodeScope));
private static final Setting.AffixSetting<Boolean> SETTING_SMTP_STARTTLS_REQUIRED =
Setting.affixKeySetting("xpack.notification.email.account.", "smtp.starttls.required",
(key) -> Setting.boolSetting(key, false, Property.Dynamic, Property.NodeScope));
private static final Setting.AffixSetting<String> SETTING_SMTP_HOST =
Setting.affixKeySetting("xpack.notification.email.account.", "smtp.host",
(key) -> Setting.simpleString(key, Property.Dynamic, Property.NodeScope));
private static final Setting.AffixSetting<Integer> SETTING_SMTP_PORT =
Setting.affixKeySetting("xpack.notification.email.account.", "smtp.port",
(key) -> Setting.intSetting(key, 587, Property.Dynamic, Property.NodeScope));
private static final Setting.AffixSetting<String> SETTING_SMTP_USER =
Setting.affixKeySetting("xpack.notification.email.account.", "smtp.user",
(key) -> Setting.simpleString(key, Property.Dynamic, Property.NodeScope));
private static final Setting.AffixSetting<String> SETTING_SMTP_PASSWORD =
Setting.affixKeySetting("xpack.notification.email.account.", "smtp.password",
(key) -> Setting.simpleString(key, Property.Dynamic, Property.NodeScope, Property.Filtered));
private static final Setting.AffixSetting<SecureString> SETTING_SECURE_PASSWORD =
Setting.affixKeySetting("xpack.notification.email.account.", "smtp.secure_password",
(key) -> SecureSetting.secureString(key, null));
private static final Setting.AffixSetting<TimeValue> SETTING_SMTP_TIMEOUT =
Setting.affixKeySetting("xpack.notification.email.account.", "smtp.timeout",
(key) -> Setting.timeSetting(key, TimeValue.timeValueMinutes(2), Property.Dynamic, Property.NodeScope));
private static final Setting.AffixSetting<TimeValue> SETTING_SMTP_CONNECTION_TIMEOUT =
Setting.affixKeySetting("xpack.notification.email.account.", "smtp.connection_timeout",
(key) -> Setting.timeSetting(key, TimeValue.timeValueMinutes(2), Property.Dynamic, Property.NodeScope));
private static final Setting.AffixSetting<TimeValue> SETTING_SMTP_WRITE_TIMEOUT =
Setting.affixKeySetting("xpack.notification.email.account.", "smtp.write_timeout",
(key) -> Setting.timeSetting(key, TimeValue.timeValueMinutes(2), Property.Dynamic, Property.NodeScope));
private static final Setting.AffixSetting<String> SETTING_SMTP_LOCAL_ADDRESS =
Setting.affixKeySetting("xpack.notification.email.account.", "smtp.local_address",
(key) -> Setting.simpleString(key, Property.Dynamic, Property.NodeScope));
private static final Setting.AffixSetting<String> SETTING_SMTP_SSL_TRUST_ADDRESS =
Setting.affixKeySetting("xpack.notification.email.account.", "smtp.ssl.trust",
(key) -> Setting.simpleString(key, Property.Dynamic, Property.NodeScope));
private static final Setting.AffixSetting<Integer> SETTING_SMTP_LOCAL_PORT =
Setting.affixKeySetting("xpack.notification.email.account.", "smtp.local_port",
(key) -> Setting.intSetting(key, 25, Property.Dynamic, Property.NodeScope));
private static final Setting.AffixSetting<Boolean> SETTING_SMTP_SEND_PARTIAL =
Setting.affixKeySetting("xpack.notification.email.account.", "smtp.send_partial",
(key) -> Setting.boolSetting(key, false, Property.Dynamic, Property.NodeScope));
private static final Setting.AffixSetting<Boolean> SETTING_SMTP_WAIT_ON_QUIT =
Setting.affixKeySetting("xpack.notification.email.account.", "smtp.wait_on_quit",
(key) -> Setting.boolSetting(key, true, Property.Dynamic, Property.NodeScope));
private final CryptoService cryptoService;
public EmailService(Settings settings, @Nullable CryptoService cryptoService, ClusterSettings clusterSettings) {
super(settings, "email", clusterSettings, EmailService.getSettings());
this.cryptoService = cryptoService;
// ensure logging of setting changes
clusterSettings.addSettingsUpdateConsumer(SETTING_DEFAULT_ACCOUNT, (s) -> {});
clusterSettings.addAffixUpdateConsumer(SETTING_PROFILE, (s, o) -> {}, (s, o) -> {});
clusterSettings.addAffixUpdateConsumer(SETTING_EMAIL_DEFAULTS, (s, o) -> {}, (s, o) -> {});
clusterSettings.addAffixUpdateConsumer(SETTING_SMTP_AUTH, (s, o) -> {}, (s, o) -> {});
clusterSettings.addAffixUpdateConsumer(SETTING_SMTP_STARTTLS_ENABLE, (s, o) -> {}, (s, o) -> {});
clusterSettings.addAffixUpdateConsumer(SETTING_SMTP_STARTTLS_REQUIRED, (s, o) -> {}, (s, o) -> {});
clusterSettings.addAffixUpdateConsumer(SETTING_SMTP_HOST, (s, o) -> {}, (s, o) -> {});
clusterSettings.addAffixUpdateConsumer(SETTING_SMTP_PORT, (s, o) -> {}, (s, o) -> {});
clusterSettings.addAffixUpdateConsumer(SETTING_SMTP_USER, (s, o) -> {}, (s, o) -> {});
clusterSettings.addAffixUpdateConsumer(SETTING_SMTP_PASSWORD, (s, o) -> {}, (s, o) -> {});
clusterSettings.addAffixUpdateConsumer(SETTING_SECURE_PASSWORD, (s, o) -> {}, (s, o) -> {});
clusterSettings.addAffixUpdateConsumer(SETTING_SMTP_TIMEOUT, (s, o) -> {}, (s, o) -> {});
clusterSettings.addAffixUpdateConsumer(SETTING_SMTP_CONNECTION_TIMEOUT, (s, o) -> {}, (s, o) -> {});
clusterSettings.addAffixUpdateConsumer(SETTING_SMTP_WRITE_TIMEOUT, (s, o) -> {}, (s, o) -> {});
clusterSettings.addAffixUpdateConsumer(SETTING_SMTP_SSL_TRUST_ADDRESS, (s, o) -> {}, (s, o) -> {});
clusterSettings.addAffixUpdateConsumer(SETTING_SMTP_LOCAL_ADDRESS, (s, o) -> {}, (s, o) -> {});
clusterSettings.addAffixUpdateConsumer(SETTING_SMTP_LOCAL_PORT, (s, o) -> {}, (s, o) -> {});
clusterSettings.addAffixUpdateConsumer(SETTING_SMTP_SEND_PARTIAL, (s, o) -> {}, (s, o) -> {});
clusterSettings.addAffixUpdateConsumer(SETTING_SMTP_WAIT_ON_QUIT, (s, o) -> {}, (s, o) -> {});
// do an initial load
reload(settings);
}
@Override
protected Account createAccount(String name, Settings accountSettings) {
Account.Config config = new Account.Config(name, accountSettings);
return new Account(config, cryptoService, logger);
}
public EmailSent send(Email email, Authentication auth, Profile profile, String accountName) throws MessagingException {
Account account = getAccount(accountName);
if (account == null) {
throw new IllegalArgumentException("failed to send email with subject [" + email.subject() + "] via account [" + accountName
+ "]. account does not exist");
}
return send(email, auth, profile, account);
}
private EmailSent send(Email email, Authentication auth, Profile profile, Account account) throws MessagingException {
assert account != null;
try {
email = account.send(email, auth, profile);
} catch (MessagingException me) {
throw new MessagingException("failed to send email with subject [" + email.subject() + "] via account [" + account.name() +
"]", me);
}
return new EmailSent(account.name(), email);
}
public static class EmailSent {
private final String account;
private final Email email;
public EmailSent(String account, Email email) {
this.account = account;
this.email = email;
}
public String account() {
return account;
}
public Email email() {
return email;
}
}
public static List<Setting<?>> getSettings() {
return Arrays.asList(SETTING_DEFAULT_ACCOUNT, SETTING_PROFILE, SETTING_EMAIL_DEFAULTS, SETTING_SMTP_AUTH, SETTING_SMTP_HOST,
SETTING_SMTP_PASSWORD, SETTING_SMTP_PORT, SETTING_SMTP_STARTTLS_ENABLE, SETTING_SMTP_USER, SETTING_SMTP_STARTTLS_REQUIRED,
SETTING_SMTP_TIMEOUT, SETTING_SMTP_CONNECTION_TIMEOUT, SETTING_SMTP_WRITE_TIMEOUT, SETTING_SMTP_LOCAL_ADDRESS,
SETTING_SMTP_LOCAL_PORT, SETTING_SMTP_SEND_PARTIAL, SETTING_SMTP_WAIT_ON_QUIT, SETTING_SMTP_SSL_TRUST_ADDRESS,
SETTING_SECURE_PASSWORD);
}
}
| gfyoung/elasticsearch | x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/notification/email/EmailService.java | Java | apache-2.0 | 10,810 |
/*
* Copyright 2000-2009 JetBrains s.r.o.
*
* 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.
*/
/*
* Created by IntelliJ IDEA.
* User: max
* Date: Jan 26, 2002
* Time: 10:48:52 PM
* To change template for new class use
* Code Style | Class Templates options (Tools | IDE Options).
*/
package com.intellij.codeInspection.dataFlow.instructions;
import com.intellij.codeInspection.dataFlow.*;
import com.intellij.codeInspection.dataFlow.value.DfaValue;
import com.intellij.psi.*;
import com.intellij.util.containers.ContainerUtil;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.Collections;
import java.util.List;
import java.util.Map;
public class MethodCallInstruction extends Instruction {
@Nullable private final PsiCall myCall;
@Nullable private final PsiType myType;
@NotNull private final PsiExpression[] myArgs;
private final boolean myShouldFlushFields;
@NotNull private final PsiElement myContext;
@Nullable private final PsiMethod myTargetMethod;
private final List<MethodContract> myContracts;
private final MethodType myMethodType;
@Nullable private final DfaValue myPrecalculatedReturnValue;
private final boolean myOfNullable;
private final boolean myVarArgCall;
private final Map<PsiExpression, Nullness> myArgRequiredNullability;
private boolean myOnlyNullArgs = true;
private boolean myOnlyNotNullArgs = true;
public enum MethodType {
BOXING, UNBOXING, REGULAR_METHOD_CALL, CAST
}
public MethodCallInstruction(@NotNull PsiExpression context, MethodType methodType, @Nullable PsiType resultType) {
myContext = context;
myContracts = Collections.emptyList();
myMethodType = methodType;
myCall = null;
myArgs = PsiExpression.EMPTY_ARRAY;
myType = resultType;
myShouldFlushFields = false;
myPrecalculatedReturnValue = null;
myTargetMethod = null;
myVarArgCall = false;
myOfNullable = false;
myArgRequiredNullability = Collections.emptyMap();
}
public MethodCallInstruction(@NotNull PsiCall call, @Nullable DfaValue precalculatedReturnValue, List<MethodContract> contracts) {
myContext = call;
myContracts = contracts;
myMethodType = MethodType.REGULAR_METHOD_CALL;
myCall = call;
final PsiExpressionList argList = call.getArgumentList();
myArgs = argList != null ? argList.getExpressions() : PsiExpression.EMPTY_ARRAY;
myType = myCall instanceof PsiCallExpression ? ((PsiCallExpression)myCall).getType() : null;
JavaResolveResult result = call.resolveMethodGenerics();
myTargetMethod = (PsiMethod)result.getElement();
PsiSubstitutor substitutor = result.getSubstitutor();
if (argList != null && myTargetMethod != null) {
PsiParameter[] parameters = myTargetMethod.getParameterList().getParameters();
myVarArgCall = isVarArgCall(myTargetMethod, substitutor, myArgs, parameters);
myArgRequiredNullability = calcArgRequiredNullability(substitutor, parameters);
} else {
myVarArgCall = false;
myArgRequiredNullability = Collections.emptyMap();
}
myShouldFlushFields = !(call instanceof PsiNewExpression && myType != null && myType.getArrayDimensions() > 0) && !isPureCall();
myPrecalculatedReturnValue = precalculatedReturnValue;
myOfNullable = call instanceof PsiMethodCallExpression && DfaOptionalSupport.resolveOfNullable((PsiMethodCallExpression)call) != null;
}
private Map<PsiExpression, Nullness> calcArgRequiredNullability(PsiSubstitutor substitutor, PsiParameter[] parameters) {
int checkedCount = Math.min(myArgs.length, parameters.length) - (myVarArgCall ? 1 : 0);
Map<PsiExpression, Nullness> map = ContainerUtil.newHashMap();
for (int i = 0; i < checkedCount; i++) {
map.put(myArgs[i], DfaPsiUtil.getElementNullability(substitutor.substitute(parameters[i].getType()), parameters[i]));
}
return map;
}
public static boolean isVarArgCall(PsiMethod method, PsiSubstitutor substitutor, PsiExpression[] args, PsiParameter[] parameters) {
if (!method.isVarArgs()) {
return false;
}
int argCount = args.length;
int paramCount = parameters.length;
if (argCount > paramCount) {
return true;
}
if (paramCount > 0 && argCount == paramCount) {
PsiType lastArgType = args[argCount - 1].getType();
if (lastArgType != null && !substitutor.substitute(parameters[paramCount - 1].getType()).isAssignableFrom(lastArgType)) {
return true;
}
}
return false;
}
private boolean isPureCall() {
if (myTargetMethod == null) return false;
return ControlFlowAnalyzer.isPure(myTargetMethod);
}
@Nullable
public PsiType getResultType() {
return myType;
}
@NotNull
public PsiExpression[] getArgs() {
return myArgs;
}
public MethodType getMethodType() {
return myMethodType;
}
public boolean shouldFlushFields() {
return myShouldFlushFields;
}
@Nullable
public PsiMethod getTargetMethod() {
return myTargetMethod;
}
public boolean isVarArgCall() {
return myVarArgCall;
}
@Nullable
public Nullness getArgRequiredNullability(@NotNull PsiExpression arg) {
return myArgRequiredNullability.get(arg);
}
public List<MethodContract> getContracts() {
return myContracts;
}
@Override
public DfaInstructionState[] accept(DataFlowRunner runner, DfaMemoryState stateBefore, InstructionVisitor visitor) {
return visitor.visitMethodCall(this, runner, stateBefore);
}
@Nullable
public PsiCall getCallExpression() {
return myCall;
}
@NotNull
public PsiElement getContext() {
return myContext;
}
@Nullable
public DfaValue getPrecalculatedReturnValue() {
return myPrecalculatedReturnValue;
}
public String toString() {
return myMethodType == MethodType.UNBOXING
? "UNBOX"
: myMethodType == MethodType.BOXING
? "BOX" :
"CALL_METHOD: " + (myCall == null ? "null" : myCall.getText());
}
public boolean updateOfNullable(DfaMemoryState memState, DfaValue arg) {
if (!myOfNullable) return false;
if (!memState.isNotNull(arg)) {
myOnlyNotNullArgs = false;
}
if (!memState.isNull(arg)) {
myOnlyNullArgs = false;
}
return true;
}
public boolean isOptionalAlwaysNullProblem() {
return myOfNullable && myOnlyNullArgs;
}
public boolean isOptionalAlwaysNotNullProblem() {
return myOfNullable && myOnlyNotNullArgs;
}
}
| idea4bsd/idea4bsd | java/java-analysis-impl/src/com/intellij/codeInspection/dataFlow/instructions/MethodCallInstruction.java | Java | apache-2.0 | 7,013 |
package com.google.api.ads.dfp.jaxws.v201408;
import java.util.ArrayList;
import java.util.List;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlSchemaType;
import javax.xml.bind.annotation.XmlType;
/**
*
* A {@code LiveStreamEvent} encapsulates all the information necessary
* to enable DAI (Dynamic Ad Insertion) into a live video stream.
*
* <p>This includes information such as the start and expected end time of
* the event, the URL of the actual content for DFP to pull and insert ads into,
* as well as the metadata necessary to generate ad requests during the event.
*
*
* <p>Java class for LiveStreamEvent complex type.
*
* <p>The following schema fragment specifies the expected content contained within this class.
*
* <pre>
* <complexType name="LiveStreamEvent">
* <complexContent>
* <restriction base="{http://www.w3.org/2001/XMLSchema}anyType">
* <sequence>
* <element name="id" type="{http://www.w3.org/2001/XMLSchema}long" minOccurs="0"/>
* <element name="name" type="{http://www.w3.org/2001/XMLSchema}string" minOccurs="0"/>
* <element name="description" type="{http://www.w3.org/2001/XMLSchema}string" minOccurs="0"/>
* <element name="status" type="{https://www.google.com/apis/ads/publisher/v201408}LiveStreamEventStatus" minOccurs="0"/>
* <element name="creationDateTime" type="{https://www.google.com/apis/ads/publisher/v201408}DateTime" minOccurs="0"/>
* <element name="lastModifiedDateTime" type="{https://www.google.com/apis/ads/publisher/v201408}DateTime" minOccurs="0"/>
* <element name="startDateTime" type="{https://www.google.com/apis/ads/publisher/v201408}DateTime" minOccurs="0"/>
* <element name="endDateTime" type="{https://www.google.com/apis/ads/publisher/v201408}DateTime" minOccurs="0"/>
* <element name="totalEstimatedConcurrentUsers" type="{http://www.w3.org/2001/XMLSchema}long" minOccurs="0"/>
* <element name="contentUrls" type="{http://www.w3.org/2001/XMLSchema}string" maxOccurs="unbounded" minOccurs="0"/>
* <element name="adTags" type="{http://www.w3.org/2001/XMLSchema}string" maxOccurs="unbounded" minOccurs="0"/>
* <element name="liveStreamEventCode" type="{http://www.w3.org/2001/XMLSchema}string" minOccurs="0"/>
* </sequence>
* </restriction>
* </complexContent>
* </complexType>
* </pre>
*
*
*/
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "LiveStreamEvent", propOrder = {
"id",
"name",
"description",
"status",
"creationDateTime",
"lastModifiedDateTime",
"startDateTime",
"endDateTime",
"totalEstimatedConcurrentUsers",
"contentUrls",
"adTags",
"liveStreamEventCode"
})
public class LiveStreamEvent {
protected Long id;
protected String name;
protected String description;
@XmlSchemaType(name = "string")
protected LiveStreamEventStatus status;
protected DateTime creationDateTime;
protected DateTime lastModifiedDateTime;
protected DateTime startDateTime;
protected DateTime endDateTime;
protected Long totalEstimatedConcurrentUsers;
protected List<String> contentUrls;
protected List<String> adTags;
protected String liveStreamEventCode;
/**
* Gets the value of the id property.
*
* @return
* possible object is
* {@link Long }
*
*/
public Long getId() {
return id;
}
/**
* Sets the value of the id property.
*
* @param value
* allowed object is
* {@link Long }
*
*/
public void setId(Long value) {
this.id = value;
}
/**
* Gets the value of the name property.
*
* @return
* possible object is
* {@link String }
*
*/
public String getName() {
return name;
}
/**
* Sets the value of the name property.
*
* @param value
* allowed object is
* {@link String }
*
*/
public void setName(String value) {
this.name = value;
}
/**
* Gets the value of the description property.
*
* @return
* possible object is
* {@link String }
*
*/
public String getDescription() {
return description;
}
/**
* Sets the value of the description property.
*
* @param value
* allowed object is
* {@link String }
*
*/
public void setDescription(String value) {
this.description = value;
}
/**
* Gets the value of the status property.
*
* @return
* possible object is
* {@link LiveStreamEventStatus }
*
*/
public LiveStreamEventStatus getStatus() {
return status;
}
/**
* Sets the value of the status property.
*
* @param value
* allowed object is
* {@link LiveStreamEventStatus }
*
*/
public void setStatus(LiveStreamEventStatus value) {
this.status = value;
}
/**
* Gets the value of the creationDateTime property.
*
* @return
* possible object is
* {@link DateTime }
*
*/
public DateTime getCreationDateTime() {
return creationDateTime;
}
/**
* Sets the value of the creationDateTime property.
*
* @param value
* allowed object is
* {@link DateTime }
*
*/
public void setCreationDateTime(DateTime value) {
this.creationDateTime = value;
}
/**
* Gets the value of the lastModifiedDateTime property.
*
* @return
* possible object is
* {@link DateTime }
*
*/
public DateTime getLastModifiedDateTime() {
return lastModifiedDateTime;
}
/**
* Sets the value of the lastModifiedDateTime property.
*
* @param value
* allowed object is
* {@link DateTime }
*
*/
public void setLastModifiedDateTime(DateTime value) {
this.lastModifiedDateTime = value;
}
/**
* Gets the value of the startDateTime property.
*
* @return
* possible object is
* {@link DateTime }
*
*/
public DateTime getStartDateTime() {
return startDateTime;
}
/**
* Sets the value of the startDateTime property.
*
* @param value
* allowed object is
* {@link DateTime }
*
*/
public void setStartDateTime(DateTime value) {
this.startDateTime = value;
}
/**
* Gets the value of the endDateTime property.
*
* @return
* possible object is
* {@link DateTime }
*
*/
public DateTime getEndDateTime() {
return endDateTime;
}
/**
* Sets the value of the endDateTime property.
*
* @param value
* allowed object is
* {@link DateTime }
*
*/
public void setEndDateTime(DateTime value) {
this.endDateTime = value;
}
/**
* Gets the value of the totalEstimatedConcurrentUsers property.
*
* @return
* possible object is
* {@link Long }
*
*/
public Long getTotalEstimatedConcurrentUsers() {
return totalEstimatedConcurrentUsers;
}
/**
* Sets the value of the totalEstimatedConcurrentUsers property.
*
* @param value
* allowed object is
* {@link Long }
*
*/
public void setTotalEstimatedConcurrentUsers(Long value) {
this.totalEstimatedConcurrentUsers = value;
}
/**
* Gets the value of the contentUrls property.
*
* <p>
* This accessor method returns a reference to the live list,
* not a snapshot. Therefore any modification you make to the
* returned list will be present inside the JAXB object.
* This is why there is not a <CODE>set</CODE> method for the contentUrls property.
*
* <p>
* For example, to add a new item, do as follows:
* <pre>
* getContentUrls().add(newItem);
* </pre>
*
*
* <p>
* Objects of the following type(s) are allowed in the list
* {@link String }
*
*
*/
public List<String> getContentUrls() {
if (contentUrls == null) {
contentUrls = new ArrayList<String>();
}
return this.contentUrls;
}
/**
* Gets the value of the adTags property.
*
* <p>
* This accessor method returns a reference to the live list,
* not a snapshot. Therefore any modification you make to the
* returned list will be present inside the JAXB object.
* This is why there is not a <CODE>set</CODE> method for the adTags property.
*
* <p>
* For example, to add a new item, do as follows:
* <pre>
* getAdTags().add(newItem);
* </pre>
*
*
* <p>
* Objects of the following type(s) are allowed in the list
* {@link String }
*
*
*/
public List<String> getAdTags() {
if (adTags == null) {
adTags = new ArrayList<String>();
}
return this.adTags;
}
/**
* Gets the value of the liveStreamEventCode property.
*
* @return
* possible object is
* {@link String }
*
*/
public String getLiveStreamEventCode() {
return liveStreamEventCode;
}
/**
* Sets the value of the liveStreamEventCode property.
*
* @param value
* allowed object is
* {@link String }
*
*/
public void setLiveStreamEventCode(String value) {
this.liveStreamEventCode = value;
}
}
| shyTNT/googleads-java-lib | modules/dfp_appengine/src/main/java/com/google/api/ads/dfp/jaxws/v201408/LiveStreamEvent.java | Java | apache-2.0 | 10,126 |
/*
* Copyright 2017 The Closure Compiler 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 com.google.javascript.jscomp;
import static com.google.common.base.Predicates.not;
import static com.google.common.collect.ImmutableList.toImmutableList;
import static com.google.common.truth.Truth.assertThat;
import static com.google.common.truth.Truth.assertWithMessage;
import com.google.common.base.Joiner;
import com.google.common.collect.ImmutableList;
import com.google.javascript.rhino.jstype.FunctionType;
import com.google.javascript.rhino.jstype.JSType;
import com.google.javascript.rhino.jstype.NamedType;
import com.google.javascript.rhino.jstype.NoType;
import com.google.javascript.rhino.jstype.ObjectType;
import com.google.javascript.rhino.jstype.UnionType;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
/**
* Tests exercising {@link CompilerOptions#assumeForwardDeclaredForMissingTypes} and {@link
* DiagnosticGroups#MISSING_SOURCES_WARNINGS}.
*/
@RunWith(JUnit4.class)
public class PartialCompilationTest {
private Compiler compiler;
/**
* Asserts that the given lines of code compile and only give errors matching the {@link
* DiagnosticGroups#MISSING_SOURCES_WARNINGS} category.
*/
private void assertPartialCompilationSucceeds(String... code) throws Exception {
compiler = new Compiler();
compiler.setErrorManager(
new BasicErrorManager() {
@Override
public void report(CheckLevel level, JSError error) {
super.report(CheckLevel.ERROR, error);
}
@Override
public void println(CheckLevel level, JSError error) {
/* no-op */
}
@Override
protected void printSummary() {
/* no-op */
}
});
CompilerOptions options = new CompilerOptions();
options.setAssumeForwardDeclaredForMissingTypes(true);
options.setStrictModeInput(true);
options.setPreserveDetailedSourceInfo(true);
CompilationLevel.ADVANCED_OPTIMIZATIONS.setOptionsForCompilationLevel(options);
compiler.init(
ImmutableList.of(),
Collections.singletonList(SourceFile.fromCode("input.js", Joiner.on('\n').join(code))),
options);
compiler.parse();
compiler.check();
ImmutableList<JSError> sourcesErrors =
compiler.getErrors().stream()
.filter(not(DiagnosticGroups.MISSING_SOURCES_WARNINGS::matches))
.collect(toImmutableList());
assertThat(sourcesErrors).isEmpty();
}
@Test
public void testUsesMissingCode() throws Exception {
assertPartialCompilationSucceeds(
"goog.provide('missing_code_user');",
"goog.require('some.thing.Missing');",
"missing_code_user.fnUsesMissingNs = function() {",
" missing_code_user.missingNamespace.foo();",
" missingTopLevelNamespace.bar();",
"};");
}
@Test
public void testMissingType_variable() throws Exception {
assertPartialCompilationSucceeds("/** @type {!some.thing.Missing} */ var foo;");
}
@Test
public void testMissingType_assignment() throws Exception {
assertPartialCompilationSucceeds(
"/** @type {!some.thing.Missing} */ var foo;", // line break
"/** @type {number} */ var bar = foo;");
}
@Test
public void testMissingRequire() throws Exception {
assertPartialCompilationSucceeds(
"goog.provide('missing_extends');", // line break
"goog.require('some.thing.Missing');");
}
@Test
public void testMissingExtends() throws Exception {
assertPartialCompilationSucceeds(
"goog.provide('missing_extends');",
"/** @constructor @extends {some.thing.Missing} */",
"missing_extends.Extends = function() {}");
}
@Test
public void testMissingExtends_template() throws Exception {
assertPartialCompilationSucceeds(
"goog.provide('missing_extends');",
"/** @constructor @extends {some.thing.Missing<string>} x */",
"missing_extends.Extends = function() {}");
}
@Test
public void testMissingType_typedefAlias() throws Exception {
assertPartialCompilationSucceeds("/** @typedef {string} */ var typedef;");
}
@Test
public void testMissingType_typedefField() throws Exception {
assertPartialCompilationSucceeds("/** @typedef {some.thing.Missing} */ var typedef;");
}
@Test
public void testMissingEs6Externs() throws Exception {
assertPartialCompilationSucceeds("let foo = {a, b};");
}
@Test
public void testUnresolvedGenerics() throws Exception {
assertPartialCompilationSucceeds(
"/** @type {!some.thing.Missing<string, !AlsoMissing<!More>>} */", "var x;");
TypedVar x = compiler.getTopScope().getSlot("x");
assertWithMessage("type %s", x.getType()).that(x.getType().isNoResolvedType()).isTrue();
NoType templatizedType = (NoType) x.getType();
assertThat(templatizedType.getReferenceName()).isEqualTo("some.thing.Missing");
ImmutableList<JSType> templateTypes = templatizedType.getTemplateTypes();
assertThat(templateTypes.get(0).isString()).isTrue();
assertThat(templateTypes.get(1).isObject()).isTrue();
ObjectType alsoMissing = (ObjectType) templateTypes.get(1);
assertThat(alsoMissing.getReferenceName()).isEqualTo("AlsoMissing");
assertThat(alsoMissing.getTemplateTypes()).hasSize(1);
ObjectType more = (ObjectType) alsoMissing.getTemplateTypes().get(0);
assertThat(more.getReferenceName()).isEqualTo("More");
}
@Test
public void testUnresolvedUnions() throws Exception {
assertPartialCompilationSucceeds("/** @type {some.thing.Foo|some.thing.Bar} */", "var x;");
TypedVar x = compiler.getTopScope().getSlot("x");
assertWithMessage("type %s", x.getType()).that(x.getType().isUnionType()).isTrue();
UnionType unionType = (UnionType) x.getType();
Collection<JSType> alternatives = unionType.getAlternates();
assertThat(alternatives).hasSize(3);
int nullTypeCount = 0;
List<String> namedTypes = new ArrayList<>();
for (JSType alternative : alternatives) {
assertThat(alternative.isNamedType() || alternative.isNullType()).isTrue();
if (alternative.isNamedType()) {
assertThat(alternative.isNoResolvedType()).isTrue();
namedTypes.add(((NamedType) alternative).getReferenceName());
}
if (alternative.isNullType()) {
nullTypeCount++;
}
}
assertThat(nullTypeCount).isEqualTo(1);
assertThat(namedTypes).containsExactly("some.thing.Foo", "some.thing.Bar");
}
@Test
public void testUnresolvedGenerics_defined() throws Exception {
assertPartialCompilationSucceeds(
"/** @param {!some.thing.Missing<string>} x */",
"function useMissing(x) {}",
"/** @const {!some.thing.Missing<string>} */",
"var x;",
"/** @constructor @template T */",
"some.thing.Missing = function () {}",
"function missingInside() {",
" useMissing(new some.thing.Missing());",
"}");
}
@Test
public void testUnresolvedBaseClassDoesNotHideFields() throws Exception {
assertPartialCompilationSucceeds(
"/** @constructor @extends {MissingBase} */",
"var Klass = function () {",
" /** @type {string} */",
" this.foo;",
"};");
TypedVar x = compiler.getTopScope().getSlot("Klass");
JSType type = x.getType();
assertThat(type.isFunctionType()).isTrue();
FunctionType fType = (FunctionType) type;
assertThat(fType.getTypeOfThis().hasProperty("foo")).isTrue();
}
}
| GoogleChromeLabs/chromeos_smart_card_connector | third_party/closure-compiler/src/test/com/google/javascript/jscomp/PartialCompilationTest.java | Java | apache-2.0 | 8,229 |
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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.apache.carbondata.core.datastore.page.encoding.dimension.legacy;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import org.apache.carbondata.core.datastore.columnar.BlockIndexerStorage;
import org.apache.carbondata.core.datastore.columnar.BlockIndexerStorageForNoInvertedIndexForShort;
import org.apache.carbondata.core.datastore.columnar.BlockIndexerStorageForShort;
import org.apache.carbondata.core.datastore.compression.Compressor;
import org.apache.carbondata.core.datastore.compression.CompressorFactory;
import org.apache.carbondata.core.datastore.page.ColumnPage;
import org.apache.carbondata.core.datastore.page.encoding.ColumnPageEncoder;
import org.apache.carbondata.core.util.ByteUtil;
import org.apache.carbondata.format.Encoding;
public class HighCardDictDimensionIndexCodec extends IndexStorageCodec {
/**
* whether this column is varchar data type(long string)
*/
private boolean isVarcharType;
public HighCardDictDimensionIndexCodec(boolean isSort, boolean isInvertedIndex,
boolean isVarcharType) {
super(isSort, isInvertedIndex);
this.isVarcharType = isVarcharType;
}
@Override
public String getName() {
return "HighCardDictDimensionIndexCodec";
}
@Override
public ColumnPageEncoder createEncoder(Map<String, String> parameter) {
return new IndexStorageEncoder() {
@Override
protected void encodeIndexStorage(ColumnPage input) {
BlockIndexerStorage<byte[][]> indexStorage;
byte[][] data = input.getByteArrayPage();
boolean isDictionary = input.isLocalDictGeneratedPage();
if (isInvertedIndex) {
indexStorage = new BlockIndexerStorageForShort(data, isDictionary, !isDictionary, isSort);
} else {
indexStorage =
new BlockIndexerStorageForNoInvertedIndexForShort(data, isDictionary);
}
byte[] flattened = ByteUtil.flatten(indexStorage.getDataPage());
Compressor compressor = CompressorFactory.getInstance().getCompressor(
input.getColumnCompressorName());
super.compressedDataPage = compressor.compressByte(flattened);
super.indexStorage = indexStorage;
}
@Override
protected List<Encoding> getEncodingList() {
List<Encoding> encodings = new ArrayList<>();
if (isVarcharType) {
encodings.add(Encoding.DIRECT_COMPRESS_VARCHAR);
} else if (indexStorage.getRowIdPageLengthInBytes() > 0) {
encodings.add(Encoding.INVERTED_INDEX);
}
if (indexStorage.getDataRlePageLengthInBytes() > 0) {
encodings.add(Encoding.RLE);
}
return encodings;
}
};
}
}
| ravipesala/incubator-carbondata | core/src/main/java/org/apache/carbondata/core/datastore/page/encoding/dimension/legacy/HighCardDictDimensionIndexCodec.java | Java | apache-2.0 | 3,503 |
package org.zstack.sdk;
import org.zstack.sdk.ImageInventory;
public class BackupStorageMigrateImageResult {
public ImageInventory inventory;
public void setInventory(ImageInventory inventory) {
this.inventory = inventory;
}
public ImageInventory getInventory() {
return this.inventory;
}
}
| zstackorg/zstack | sdk/src/main/java/org/zstack/sdk/BackupStorageMigrateImageResult.java | Java | apache-2.0 | 330 |
/*
*
* ***** BEGIN LICENSE BLOCK *****
* Version: MPL 1.1/GPL 2.0
*
* The contents of this file are subject to the Mozilla Public License Version
* 1.1 (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.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Original Code is Rhino code, released
* May 6, 1999.
*
* The Initial Developer of the Original Code is
* Netscape Communications Corporation.
* Portions created by the Initial Developer are Copyright (C) 1997-1999
* the Initial Developer. All Rights Reserved.
*
* Contributor(s):
* Bob Jervis
* Google Inc.
*
* Alternatively, the contents of this file may be used under the terms of
* the GNU General Public License Version 2 or later (the "GPL"), in which
* case the provisions of the GPL are applicable instead of those above. If
* you wish to allow use of your version of this file only under the terms of
* the GPL and not to allow others to use your version of this file under the
* MPL, indicate your decision by deleting the provisions above and replacing
* them with the notice and other provisions required by the GPL. If you do
* not delete the provisions above, a recipient may use your version of this
* file under either the MPL or the GPL.
*
* ***** END LICENSE BLOCK ***** */
package com.google.javascript.rhino;
import java.util.ArrayList;
import java.util.List;
/**
* A simple {@link ErrorReporter} that collects warnings and errors and makes
* them accessible via {@link #errors()} and {@link #warnings()}.
*
*
*/
public class SimpleErrorReporter implements ErrorReporter {
private List<String> warnings = null;
private List<String> errors = null;
public void warning(String message, String sourceName, int line,
String lineSource, int lineOffset)
{
if (warnings == null) {
warnings = new ArrayList<String>();
}
warnings.add(formatDetailedMessage(
message, sourceName, line, lineSource, lineOffset));
}
public void error(String message, String sourceName, int line,
String lineSource, int lineOffset)
{
if (errors == null) {
errors = new ArrayList<String>();
}
errors.add(formatDetailedMessage(
message, sourceName, line, lineSource, lineOffset));
}
public EvaluatorException runtimeError(
String message, String sourceName, int line, String lineSource,
int lineOffset)
{
return new EvaluatorException(
message, sourceName, line, lineSource, lineOffset);
}
/**
* Returns the list of errors, or {@code null} if there were none.
*/
public List<String> errors()
{
return errors;
}
/**
* Returns the list of warnings, or {@code null} if there were none.
*/
public List<String> warnings()
{
return warnings;
}
private String formatDetailedMessage(
String message, String sourceName, int line, String lineSource,
int lineOffset)
{
RhinoException e = new RhinoException(message);
if (sourceName != null) {
e.initSourceName(sourceName);
}
if (lineSource != null) {
e.initLineSource(lineSource);
}
if (line > 0) {
e.initLineNumber(line);
}
if (lineOffset > 0) {
e.initColumnNumber(lineOffset);
}
return e.getMessage();
}
}
| ehsan/js-symbolic-executor | closure-compiler/src/com/google/javascript/rhino/SimpleErrorReporter.java | Java | apache-2.0 | 3,775 |
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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.apache.hadoop.hdfs.server.namenode;
import static org.apache.hadoop.fs.permission.AclEntryScope.*;
import static org.apache.hadoop.fs.permission.AclEntryType.*;
import static org.apache.hadoop.fs.permission.FsAction.*;
import static org.apache.hadoop.hdfs.server.namenode.AclTestHelpers.*;
import static org.apache.hadoop.test.MetricsAsserts.assertCounter;
import static org.apache.hadoop.test.MetricsAsserts.getMetrics;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import java.io.BufferedInputStream;
import java.io.ByteArrayInputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FilenameFilter;
import java.io.IOException;
import java.io.InputStream;
import java.io.RandomAccessFile;
import java.net.URI;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Random;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.apache.commons.io.FileUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.ChecksumException;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.FileUtil;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.fs.permission.AclEntry;
import org.apache.hadoop.fs.permission.FsPermission;
import org.apache.hadoop.fs.permission.PermissionStatus;
import org.apache.hadoop.hdfs.DFSConfigKeys;
import org.apache.hadoop.hdfs.DFSInotifyEventInputStream;
import org.apache.hadoop.hdfs.DistributedFileSystem;
import org.apache.hadoop.hdfs.HdfsConfiguration;
import org.apache.hadoop.hdfs.MiniDFSCluster;
import org.apache.hadoop.hdfs.protocol.HdfsFileStatus;
import org.apache.hadoop.hdfs.server.blockmanagement.BlockInfo;
import org.apache.hadoop.hdfs.server.common.HdfsServerConstants;
import org.apache.hadoop.hdfs.server.common.Storage.StorageDirectory;
import org.apache.hadoop.hdfs.server.namenode.NNStorage.NameNodeDirType;
import org.apache.hadoop.hdfs.server.namenode.metrics.NameNodeMetrics;
import org.apache.hadoop.hdfs.server.protocol.NamespaceInfo;
import org.apache.hadoop.hdfs.util.XMLUtils.InvalidXmlException;
import org.apache.hadoop.hdfs.util.XMLUtils.Stanza;
import org.apache.hadoop.io.IOUtils;
import org.apache.hadoop.test.GenericTestUtils;
import org.apache.hadoop.test.PathUtils;
import org.apache.hadoop.util.StringUtils;
import org.apache.hadoop.util.Time;
import org.apache.log4j.Level;
import org.apache.log4j.AppenderSkeleton;
import org.apache.log4j.LogManager;
import org.apache.log4j.spi.LoggingEvent;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameters;
import org.mockito.Mockito;
import org.xml.sax.ContentHandler;
import org.xml.sax.SAXException;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Lists;
/**
* This class tests the creation and validation of a checkpoint.
*/
@RunWith(Parameterized.class)
public class TestEditLog {
static {
GenericTestUtils.setLogLevel(FSEditLog.LOG, Level.ALL);
}
@Parameters
public static Collection<Object[]> data() {
Collection<Object[]> params = new ArrayList<Object[]>();
params.add(new Object[]{ Boolean.FALSE });
params.add(new Object[]{ Boolean.TRUE });
return params;
}
private static boolean useAsyncEditLog;
public TestEditLog(Boolean async) {
useAsyncEditLog = async;
}
public static Configuration getConf() {
Configuration conf = new HdfsConfiguration();
conf.setBoolean(DFSConfigKeys.DFS_NAMENODE_EDITS_ASYNC_LOGGING,
useAsyncEditLog);
return conf;
}
/**
* A garbage mkdir op which is used for testing
* {@link EditLogFileInputStream#scanEditLog(File, long, boolean)}
*/
public static class GarbageMkdirOp extends FSEditLogOp {
public GarbageMkdirOp() {
super(FSEditLogOpCodes.OP_MKDIR);
}
@Override
void resetSubFields() {
// nop
}
@Override
void readFields(DataInputStream in, int logVersion) throws IOException {
throw new IOException("cannot decode GarbageMkdirOp");
}
@Override
public void writeFields(DataOutputStream out) throws IOException {
// write in some garbage content
Random random = new Random();
byte[] content = new byte[random.nextInt(16) + 1];
random.nextBytes(content);
out.write(content);
}
@Override
protected void toXml(ContentHandler contentHandler) throws SAXException {
throw new UnsupportedOperationException(
"Not supported for GarbageMkdirOp");
}
@Override
void fromXml(Stanza st) throws InvalidXmlException {
throw new UnsupportedOperationException(
"Not supported for GarbageMkdirOp");
}
}
static final Log LOG = LogFactory.getLog(TestEditLog.class);
static final int NUM_DATA_NODES = 0;
// This test creates NUM_THREADS threads and each thread does
// 2 * NUM_TRANSACTIONS Transactions concurrently.
static final int NUM_TRANSACTIONS = 100;
static final int NUM_THREADS = 100;
static final File TEST_DIR = PathUtils.getTestDir(TestEditLog.class);
/** An edits log with 3 edits from 0.20 - the result of
* a fresh namesystem followed by hadoop fs -touchz /myfile */
static final byte[] HADOOP20_SOME_EDITS =
StringUtils.hexStringToByte((
"ffff ffed 0a00 0000 0000 03fa e100 0000" +
"0005 0007 2f6d 7966 696c 6500 0133 000d" +
"3132 3932 3331 3634 3034 3138 3400 0d31" +
"3239 3233 3136 3430 3431 3834 0009 3133" +
"3432 3137 3732 3800 0000 0004 746f 6464" +
"0a73 7570 6572 6772 6f75 7001 a400 1544" +
"4653 436c 6965 6e74 5f2d 3136 3136 3535" +
"3738 3931 000b 3137 322e 3239 2e35 2e33" +
"3209 0000 0005 0007 2f6d 7966 696c 6500" +
"0133 000d 3132 3932 3331 3634 3034 3138" +
"3400 0d31 3239 3233 3136 3430 3431 3834" +
"0009 3133 3432 3137 3732 3800 0000 0004" +
"746f 6464 0a73 7570 6572 6772 6f75 7001" +
"a4ff 0000 0000 0000 0000 0000 0000 0000"
).replace(" ",""));
static {
// No need to fsync for the purposes of tests. This makes
// the tests run much faster.
EditLogFileOutputStream.setShouldSkipFsyncForTesting(true);
}
static final byte TRAILER_BYTE = FSEditLogOpCodes.OP_INVALID.getOpCode();
private static final int CHECKPOINT_ON_STARTUP_MIN_TXNS = 100;
//
// an object that does a bunch of transactions
//
static class Transactions implements Runnable {
final FSNamesystem namesystem;
final int numTransactions;
final short replication = 3;
final long blockSize = 64;
final int startIndex;
Transactions(FSNamesystem ns, int numTx, int startIdx) {
namesystem = ns;
numTransactions = numTx;
startIndex = startIdx;
}
// add a bunch of transactions.
@Override
public void run() {
PermissionStatus p = namesystem.createFsOwnerPermissions(
new FsPermission((short)0777));
FSEditLog editLog = namesystem.getEditLog();
for (int i = 0; i < numTransactions; i++) {
INodeFile inode = new INodeFile(namesystem.dir.allocateNewInodeId(), null,
p, 0L, 0L, BlockInfo.EMPTY_ARRAY, replication, blockSize);
inode.toUnderConstruction("", "");
editLog.logOpenFile("/filename" + (startIndex + i), inode, false, false);
editLog.logCloseFile("/filename" + (startIndex + i), inode);
editLog.logSync();
}
}
}
/**
* Construct FSEditLog with default configuration, taking editDirs from NNStorage
*
* @param storage Storage object used by namenode
*/
private static FSEditLog getFSEditLog(NNStorage storage) throws IOException {
Configuration conf = getConf();
// Make sure the edits dirs are set in the provided configuration object.
conf.set(DFSConfigKeys.DFS_NAMENODE_EDITS_DIR_KEY,
StringUtils.join(",", storage.getEditsDirectories()));
FSEditLog log = FSEditLog.newInstance(
conf, storage, FSNamesystem.getNamespaceEditsDirs(conf));
return log;
}
/**
* Test case for an empty edit log from a prior version of Hadoop.
*/
@Test
public void testPreTxIdEditLogNoEdits() throws Exception {
FSNamesystem namesys = Mockito.mock(FSNamesystem.class);
namesys.dir = Mockito.mock(FSDirectory.class);
long numEdits = testLoad(
StringUtils.hexStringToByte("ffffffed"), // just version number
namesys);
assertEquals(0, numEdits);
}
/**
* Test case for loading a very simple edit log from a format
* prior to the inclusion of edit transaction IDs in the log.
*/
@Test
public void testPreTxidEditLogWithEdits() throws Exception {
Configuration conf = getConf();
MiniDFSCluster cluster = null;
try {
cluster = new MiniDFSCluster.Builder(conf).numDataNodes(0).build();
cluster.waitActive();
final FSNamesystem namesystem = cluster.getNamesystem();
long numEdits = testLoad(HADOOP20_SOME_EDITS, namesystem);
assertEquals(3, numEdits);
// Sanity check the edit
HdfsFileStatus fileInfo =
namesystem.getFileInfo("/myfile", false, false, false);
assertEquals("supergroup", fileInfo.getGroup());
assertEquals(3, fileInfo.getReplication());
} finally {
if (cluster != null) { cluster.shutdown(); }
}
}
private long testLoad(byte[] data, FSNamesystem namesys) throws IOException {
FSEditLogLoader loader = new FSEditLogLoader(namesys, 0);
return loader.loadFSEdits(new EditLogByteInputStream(data), 1);
}
/**
* Simple test for writing to and rolling the edit log.
*/
@Test
public void testSimpleEditLog() throws IOException {
// start a cluster
Configuration conf = getConf();
MiniDFSCluster cluster = null;
FileSystem fileSys = null;
try {
cluster = new MiniDFSCluster.Builder(conf).numDataNodes(NUM_DATA_NODES).build();
cluster.waitActive();
fileSys = cluster.getFileSystem();
final FSNamesystem namesystem = cluster.getNamesystem();
FSImage fsimage = namesystem.getFSImage();
final FSEditLog editLog = fsimage.getEditLog();
assertExistsInStorageDirs(
cluster, NameNodeDirType.EDITS,
NNStorage.getInProgressEditsFileName(1));
editLog.logSetReplication("fakefile", (short) 1);
editLog.logSync();
editLog.rollEditLog(NameNodeLayoutVersion.CURRENT_LAYOUT_VERSION);
assertExistsInStorageDirs(
cluster, NameNodeDirType.EDITS,
NNStorage.getFinalizedEditsFileName(1,3));
assertExistsInStorageDirs(
cluster, NameNodeDirType.EDITS,
NNStorage.getInProgressEditsFileName(4));
editLog.logSetReplication("fakefile", (short) 2);
editLog.logSync();
editLog.close();
} finally {
if(fileSys != null) fileSys.close();
if(cluster != null) cluster.shutdown();
}
}
/**
* Tests transaction logging in dfs.
*/
@Test
public void testMultiThreadedEditLog() throws IOException {
testEditLog(2048);
// force edit buffer to automatically sync on each log of edit log entry
testEditLog(1);
}
private void assertExistsInStorageDirs(MiniDFSCluster cluster,
NameNodeDirType dirType,
String filename) {
NNStorage storage = cluster.getNamesystem().getFSImage().getStorage();
for (StorageDirectory sd : storage.dirIterable(dirType)) {
File f = new File(sd.getCurrentDir(), filename);
assertTrue("Expect that " + f + " exists", f.exists());
}
}
/**
* Test edit log with different initial buffer size
*
* @param initialSize initial edit log buffer size
* @throws IOException
*/
private void testEditLog(int initialSize) throws IOException {
// start a cluster
Configuration conf = getConf();
MiniDFSCluster cluster = null;
FileSystem fileSys = null;
try {
cluster = new MiniDFSCluster.Builder(conf).numDataNodes(NUM_DATA_NODES).build();
cluster.waitActive();
fileSys = cluster.getFileSystem();
final FSNamesystem namesystem = cluster.getNamesystem();
for (Iterator<URI> it = cluster.getNameDirs(0).iterator(); it.hasNext(); ) {
File dir = new File(it.next().getPath());
System.out.println(dir);
}
FSImage fsimage = namesystem.getFSImage();
FSEditLog editLog = fsimage.getEditLog();
// set small size of flush buffer
editLog.setOutputBufferCapacity(initialSize);
// Roll log so new output buffer size takes effect
// we should now be writing to edits_inprogress_3
fsimage.rollEditLog(NameNodeLayoutVersion.CURRENT_LAYOUT_VERSION);
// Remember the current lastInodeId and will reset it back to test
// loading editlog segments.The transactions in the following allocate new
// inode id to write to editlogs but doesn't create ionde in namespace
long originalLastInodeId = namesystem.dir.getLastInodeId();
// Create threads and make them run transactions concurrently.
Thread threadId[] = new Thread[NUM_THREADS];
for (int i = 0; i < NUM_THREADS; i++) {
Transactions trans =
new Transactions(namesystem, NUM_TRANSACTIONS, i*NUM_TRANSACTIONS);
threadId[i] = new Thread(trans, "TransactionThread-" + i);
threadId[i].start();
}
// wait for all transactions to get over
for (int i = 0; i < NUM_THREADS; i++) {
try {
threadId[i].join();
} catch (InterruptedException e) {
i--; // retry
}
}
// Reopen some files as for append
Transactions trans =
new Transactions(namesystem, NUM_TRANSACTIONS, NUM_TRANSACTIONS / 2);
trans.run();
// Roll another time to finalize edits_inprogress_3
fsimage.rollEditLog(NameNodeLayoutVersion.CURRENT_LAYOUT_VERSION);
long expectedTxns = ((NUM_THREADS+1) * 2 * NUM_TRANSACTIONS) + 2; // +2 for start/end txns
// Verify that we can read in all the transactions that we have written.
// If there were any corruptions, it is likely that the reading in
// of these transactions will throw an exception.
//
namesystem.dir.resetLastInodeIdWithoutChecking(originalLastInodeId);
for (Iterator<StorageDirectory> it =
fsimage.getStorage().dirIterator(NameNodeDirType.EDITS); it.hasNext();) {
FSEditLogLoader loader = new FSEditLogLoader(namesystem, 0);
File editFile = NNStorage.getFinalizedEditsFile(it.next(), 3,
3 + expectedTxns - 1);
assertTrue("Expect " + editFile + " exists", editFile.exists());
System.out.println("Verifying file: " + editFile);
long numEdits = loader.loadFSEdits(
new EditLogFileInputStream(editFile), 3);
int numLeases = namesystem.leaseManager.countLease();
System.out.println("Number of outstanding leases " + numLeases);
assertEquals(0, numLeases);
assertTrue("Verification for " + editFile + " failed. " +
"Expected " + expectedTxns + " transactions. "+
"Found " + numEdits + " transactions.",
numEdits == expectedTxns);
}
} finally {
try {
if(fileSys != null) fileSys.close();
if(cluster != null) cluster.shutdown();
} catch (Throwable t) {
LOG.error("Couldn't shut down cleanly", t);
}
}
}
private void doLogEdit(ExecutorService exec, final FSEditLog log,
final String filename) throws Exception
{
exec.submit(new Callable<Void>() {
@Override
public Void call() {
log.logSetReplication(filename, (short)1);
return null;
}
}).get();
}
private void doCallLogSync(ExecutorService exec, final FSEditLog log)
throws Exception
{
exec.submit(new Callable<Void>() {
@Override
public Void call() {
log.logSync();
return null;
}
}).get();
}
private void doCallLogSyncAll(ExecutorService exec, final FSEditLog log)
throws Exception
{
exec.submit(new Callable<Void>() {
@Override
public Void call() throws Exception {
log.logSyncAll();
return null;
}
}).get();
}
@Test
public void testSyncBatching() throws Exception {
if (useAsyncEditLog) {
// semantics are completely differently since edits will be auto-synced
return;
}
// start a cluster
Configuration conf = getConf();
MiniDFSCluster cluster = null;
FileSystem fileSys = null;
ExecutorService threadA = Executors.newSingleThreadExecutor();
ExecutorService threadB = Executors.newSingleThreadExecutor();
try {
cluster = new MiniDFSCluster.Builder(conf).numDataNodes(NUM_DATA_NODES).build();
cluster.waitActive();
fileSys = cluster.getFileSystem();
final FSNamesystem namesystem = cluster.getNamesystem();
FSImage fsimage = namesystem.getFSImage();
final FSEditLog editLog = fsimage.getEditLog();
assertEquals("should start with only the BEGIN_LOG_SEGMENT txn synced",
1, editLog.getSyncTxId());
// Log an edit from thread A
doLogEdit(threadA, editLog, "thread-a 1");
assertEquals("logging edit without syncing should do not affect txid",
1, editLog.getSyncTxId());
// Log an edit from thread B
doLogEdit(threadB, editLog, "thread-b 1");
assertEquals("logging edit without syncing should do not affect txid",
1, editLog.getSyncTxId());
// Now ask to sync edit from B, which should sync both edits.
doCallLogSync(threadB, editLog);
assertEquals("logSync from second thread should bump txid up to 3",
3, editLog.getSyncTxId());
// Now ask to sync edit from A, which was already batched in - thus
// it should increment the batch count metric
doCallLogSync(threadA, editLog);
assertEquals("logSync from first thread shouldn't change txid",
3, editLog.getSyncTxId());
//Should have incremented the batch count exactly once
assertCounter("TransactionsBatchedInSync", 1L,
getMetrics("NameNodeActivity"));
} finally {
threadA.shutdown();
threadB.shutdown();
if(fileSys != null) fileSys.close();
if(cluster != null) cluster.shutdown();
}
}
/**
* Test what happens with the following sequence:
*
* Thread A writes edit
* Thread B calls logSyncAll
* calls close() on stream
* Thread A calls logSync
*
* This sequence is legal and can occur if enterSafeMode() is closely
* followed by saveNamespace.
*/
@Test
public void testBatchedSyncWithClosedLogs() throws Exception {
// start a cluster
Configuration conf = getConf();
MiniDFSCluster cluster = null;
FileSystem fileSys = null;
ExecutorService threadA = Executors.newSingleThreadExecutor();
ExecutorService threadB = Executors.newSingleThreadExecutor();
try {
cluster = new MiniDFSCluster.Builder(conf).numDataNodes(NUM_DATA_NODES).build();
cluster.waitActive();
fileSys = cluster.getFileSystem();
final FSNamesystem namesystem = cluster.getNamesystem();
FSImage fsimage = namesystem.getFSImage();
final FSEditLog editLog = fsimage.getEditLog();
// Log an edit from thread A
doLogEdit(threadA, editLog, "thread-a 1");
// async log is doing batched syncs in background. logSync just ensures
// the edit is durable, so the txid may increase prior to sync
if (!useAsyncEditLog) {
assertEquals("logging edit without syncing should do not affect txid",
1, editLog.getSyncTxId());
}
// logSyncAll in Thread B
doCallLogSyncAll(threadB, editLog);
assertEquals("logSyncAll should sync thread A's transaction",
2, editLog.getSyncTxId());
// Close edit log
editLog.close();
// Ask thread A to finish sync (which should be a no-op)
doCallLogSync(threadA, editLog);
} finally {
threadA.shutdown();
threadB.shutdown();
if(fileSys != null) fileSys.close();
if(cluster != null) cluster.shutdown();
}
}
@Test
public void testEditChecksum() throws Exception {
// start a cluster
Configuration conf = getConf();
MiniDFSCluster cluster = null;
FileSystem fileSys = null;
cluster = new MiniDFSCluster.Builder(conf).numDataNodes(NUM_DATA_NODES).build();
cluster.waitActive();
fileSys = cluster.getFileSystem();
final FSNamesystem namesystem = cluster.getNamesystem();
FSImage fsimage = namesystem.getFSImage();
final FSEditLog editLog = fsimage.getEditLog();
fileSys.mkdirs(new Path("/tmp"));
Iterator<StorageDirectory> iter = fsimage.getStorage().
dirIterator(NameNodeDirType.EDITS);
LinkedList<StorageDirectory> sds = new LinkedList<StorageDirectory>();
while (iter.hasNext()) {
sds.add(iter.next());
}
editLog.close();
cluster.shutdown();
for (StorageDirectory sd : sds) {
File editFile = NNStorage.getFinalizedEditsFile(sd, 1, 3);
assertTrue(editFile.exists());
long fileLen = editFile.length();
LOG.debug("Corrupting Log File: " + editFile + " len: " + fileLen);
RandomAccessFile rwf = new RandomAccessFile(editFile, "rw");
rwf.seek(fileLen-4); // seek to checksum bytes
int b = rwf.readInt();
rwf.seek(fileLen-4);
rwf.writeInt(b+1);
rwf.close();
}
try {
cluster = new MiniDFSCluster.Builder(conf).numDataNodes(NUM_DATA_NODES).format(false).build();
fail("should not be able to start");
} catch (IOException e) {
// expected
assertNotNull("Cause of exception should be ChecksumException", e.getCause());
assertEquals("Cause of exception should be ChecksumException",
ChecksumException.class, e.getCause().getClass());
}
}
/**
* Test what happens if the NN crashes when it has has started but
* had no transactions written.
*/
@Test
public void testCrashRecoveryNoTransactions() throws Exception {
testCrashRecovery(0);
}
/**
* Test what happens if the NN crashes when it has has started and
* had a few transactions written
*/
@Test
public void testCrashRecoveryWithTransactions() throws Exception {
testCrashRecovery(150);
}
/**
* Do a test to make sure the edit log can recover edits even after
* a non-clean shutdown. This does a simulated crash by copying over
* the edits directory while the NN is still running, then shutting it
* down, and restoring that edits directory.
*/
private void testCrashRecovery(int numTransactions) throws Exception {
MiniDFSCluster cluster = null;
Configuration conf = getConf();
conf.setInt(DFSConfigKeys.DFS_NAMENODE_CHECKPOINT_TXNS_KEY,
CHECKPOINT_ON_STARTUP_MIN_TXNS);
try {
LOG.info("\n===========================================\n" +
"Starting empty cluster");
cluster = new MiniDFSCluster.Builder(conf)
.numDataNodes(NUM_DATA_NODES)
.format(true)
.build();
cluster.waitActive();
FileSystem fs = cluster.getFileSystem();
for (int i = 0; i < numTransactions; i++) {
fs.mkdirs(new Path("/test" + i));
}
// Directory layout looks like:
// test/data/dfs/nameN/current/{fsimage_N,edits_...}
File nameDir = new File(cluster.getNameDirs(0).iterator().next().getPath());
File dfsDir = nameDir.getParentFile();
assertEquals(dfsDir.getName(), "dfs"); // make sure we got right dir
LOG.info("Copying data directory aside to a hot backup");
File backupDir = new File(dfsDir.getParentFile(), "dfs.backup-while-running");
FileUtils.copyDirectory(dfsDir, backupDir);
LOG.info("Shutting down cluster #1");
cluster.shutdown();
cluster = null;
// Now restore the backup
FileUtil.fullyDeleteContents(dfsDir);
dfsDir.delete();
backupDir.renameTo(dfsDir);
// Directory layout looks like:
// test/data/dfs/nameN/current/{fsimage_N,edits_...}
File currentDir = new File(nameDir, "current");
// We should see the file as in-progress
File editsFile = new File(currentDir,
NNStorage.getInProgressEditsFileName(1));
assertTrue("Edits file " + editsFile + " should exist", editsFile.exists());
File imageFile = FSImageTestUtil.findNewestImageFile(
currentDir.getAbsolutePath());
assertNotNull("No image found in " + nameDir, imageFile);
assertEquals(NNStorage.getImageFileName(0), imageFile.getName());
// Try to start a new cluster
LOG.info("\n===========================================\n" +
"Starting same cluster after simulated crash");
cluster = new MiniDFSCluster.Builder(conf)
.numDataNodes(NUM_DATA_NODES)
.format(false)
.build();
cluster.waitActive();
// We should still have the files we wrote prior to the simulated crash
fs = cluster.getFileSystem();
for (int i = 0; i < numTransactions; i++) {
assertTrue(fs.exists(new Path("/test" + i)));
}
long expectedTxId;
if (numTransactions > CHECKPOINT_ON_STARTUP_MIN_TXNS) {
// It should have saved a checkpoint on startup since there
// were more unfinalized edits than configured
expectedTxId = numTransactions + 1;
} else {
// otherwise, it shouldn't have made a checkpoint
expectedTxId = 0;
}
imageFile = FSImageTestUtil.findNewestImageFile(
currentDir.getAbsolutePath());
assertNotNull("No image found in " + nameDir, imageFile);
assertEquals(NNStorage.getImageFileName(expectedTxId),
imageFile.getName());
// Started successfully. Shut it down and make sure it can restart.
cluster.shutdown();
cluster = null;
cluster = new MiniDFSCluster.Builder(conf)
.numDataNodes(NUM_DATA_NODES)
.format(false)
.build();
cluster.waitActive();
} finally {
if (cluster != null) {
cluster.shutdown();
}
}
}
// should succeed - only one corrupt log dir
@Test
public void testCrashRecoveryEmptyLogOneDir() throws Exception {
doTestCrashRecoveryEmptyLog(false, true, true);
}
// should fail - seen_txid updated to 3, but no log dir contains txid 3
@Test
public void testCrashRecoveryEmptyLogBothDirs() throws Exception {
doTestCrashRecoveryEmptyLog(true, true, false);
}
// should succeed - only one corrupt log dir
@Test
public void testCrashRecoveryEmptyLogOneDirNoUpdateSeenTxId()
throws Exception {
doTestCrashRecoveryEmptyLog(false, false, true);
}
// should succeed - both log dirs corrupt, but seen_txid never updated
@Test
public void testCrashRecoveryEmptyLogBothDirsNoUpdateSeenTxId()
throws Exception {
doTestCrashRecoveryEmptyLog(true, false, true);
}
/**
* Test that the NN handles the corruption properly
* after it crashes just after creating an edit log
* (ie before writing START_LOG_SEGMENT). In the case
* that all logs have this problem, it should mark them
* as corrupt instead of trying to finalize them.
*
* @param inBothDirs if true, there will be a truncated log in
* both of the edits directories. If false, the truncated log
* will only be in one of the directories. In both cases, the
* NN should fail to start up, because it's aware that txid 3
* was reached, but unable to find a non-corrupt log starting there.
* @param updateTransactionIdFile if true update the seen_txid file.
* If false, it will not be updated. This will simulate a case where
* the NN crashed between creating the new segment and updating the
* seen_txid file.
* @param shouldSucceed true if the test is expected to succeed.
*/
private void doTestCrashRecoveryEmptyLog(boolean inBothDirs,
boolean updateTransactionIdFile, boolean shouldSucceed)
throws Exception {
// start a cluster
Configuration conf = getConf();
MiniDFSCluster cluster = null;
cluster = new MiniDFSCluster.Builder(conf)
.numDataNodes(NUM_DATA_NODES).build();
cluster.shutdown();
Collection<URI> editsDirs = cluster.getNameEditsDirs(0);
for (URI uri : editsDirs) {
File dir = new File(uri.getPath());
File currentDir = new File(dir, "current");
// We should start with only the finalized edits_1-2
GenericTestUtils.assertGlobEquals(currentDir, "edits_.*",
NNStorage.getFinalizedEditsFileName(1, 2));
// Make a truncated edits_3_inprogress
File log = new File(currentDir,
NNStorage.getInProgressEditsFileName(3));
EditLogFileOutputStream stream = new EditLogFileOutputStream(conf, log, 1024);
try {
stream.create(NameNodeLayoutVersion.CURRENT_LAYOUT_VERSION);
if (!inBothDirs) {
break;
}
NNStorage storage = new NNStorage(conf,
Collections.<URI>emptyList(),
Lists.newArrayList(uri));
if (updateTransactionIdFile) {
storage.writeTransactionIdFileToStorage(3);
}
storage.close();
} finally {
stream.close();
}
}
try {
cluster = new MiniDFSCluster.Builder(conf)
.numDataNodes(NUM_DATA_NODES).format(false).build();
if (!shouldSucceed) {
fail("Should not have succeeded in startin cluster");
}
} catch (IOException ioe) {
if (shouldSucceed) {
LOG.info("Should have succeeded in starting cluster, but failed", ioe);
throw ioe;
} else {
GenericTestUtils.assertExceptionContains(
"Gap in transactions. Expected to be able to read up until " +
"at least txid 3 but unable to find any edit logs containing " +
"txid 3", ioe);
}
} finally {
cluster.shutdown();
}
}
private static class EditLogByteInputStream extends EditLogInputStream {
private final InputStream input;
private final long len;
private int version;
private FSEditLogOp.Reader reader = null;
private FSEditLogLoader.PositionTrackingInputStream tracker = null;
public EditLogByteInputStream(byte[] data) throws IOException {
len = data.length;
input = new ByteArrayInputStream(data);
BufferedInputStream bin = new BufferedInputStream(input);
DataInputStream in = new DataInputStream(bin);
version = EditLogFileInputStream.readLogVersion(in, true);
tracker = new FSEditLogLoader.PositionTrackingInputStream(in);
in = new DataInputStream(tracker);
reader = FSEditLogOp.Reader.create(in, tracker, version);
}
@Override
public long getFirstTxId() {
return HdfsServerConstants.INVALID_TXID;
}
@Override
public long getLastTxId() {
return HdfsServerConstants.INVALID_TXID;
}
@Override
public long length() throws IOException {
return len;
}
@Override
public long getPosition() {
return tracker.getPos();
}
@Override
protected FSEditLogOp nextOp() throws IOException {
return reader.readOp(false);
}
@Override
public int getVersion(boolean verifyVersion) throws IOException {
return version;
}
@Override
public void close() throws IOException {
input.close();
}
@Override
public String getName() {
return "AnonEditLogByteInputStream";
}
@Override
public boolean isInProgress() {
return true;
}
@Override
public void setMaxOpSize(int maxOpSize) {
reader.setMaxOpSize(maxOpSize);
}
@Override public boolean isLocalLog() {
return true;
}
}
@Test
public void testFailedOpen() throws Exception {
File logDir = new File(TEST_DIR, "testFailedOpen");
logDir.mkdirs();
FSEditLog log = FSImageTestUtil.createStandaloneEditLog(logDir);
try {
FileUtil.setWritable(logDir, false);
log.openForWrite(NameNodeLayoutVersion.CURRENT_LAYOUT_VERSION);
fail("Did no throw exception on only having a bad dir");
} catch (IOException ioe) {
GenericTestUtils.assertExceptionContains(
"too few journals successfully started", ioe);
} finally {
FileUtil.setWritable(logDir, true);
log.close();
}
}
/**
* Regression test for HDFS-1112/HDFS-3020. Ensures that, even if
* logSync isn't called periodically, the edit log will sync itself.
*/
@Test
public void testAutoSync() throws Exception {
File logDir = new File(TEST_DIR, "testAutoSync");
logDir.mkdirs();
FSEditLog log = FSImageTestUtil.createStandaloneEditLog(logDir);
String oneKB = StringUtils.byteToHexString(
new byte[500]);
try {
log.openForWrite(NameNodeLayoutVersion.CURRENT_LAYOUT_VERSION);
NameNodeMetrics mockMetrics = Mockito.mock(NameNodeMetrics.class);
log.setMetricsForTests(mockMetrics);
for (int i = 0; i < 400; i++) {
log.logDelete(oneKB, 1L, false);
}
// After ~400KB, we're still within the 512KB buffer size
Mockito.verify(mockMetrics, Mockito.times(0)).addSync(Mockito.anyLong());
// After ~400KB more, we should have done an automatic sync
for (int i = 0; i < 400; i++) {
log.logDelete(oneKB, 1L, false);
}
Mockito.verify(mockMetrics, Mockito.times(1)).addSync(Mockito.anyLong());
} finally {
log.close();
}
}
/**
* Tests the getEditLogManifest function using mock storage for a number
* of different situations.
*/
@Test
public void testEditLogManifestMocks() throws IOException {
NNStorage storage;
FSEditLog log;
// Simple case - different directories have the same
// set of logs, with an in-progress one at end
storage = mockStorageWithEdits(
"[1,100]|[101,200]|[201,]",
"[1,100]|[101,200]|[201,]");
log = getFSEditLog(storage);
log.initJournalsForWrite();
assertEquals("[[1,100], [101,200]] CommittedTxId: 200",
log.getEditLogManifest(1).toString());
assertEquals("[[101,200]] CommittedTxId: 200",
log.getEditLogManifest(101).toString());
// Another simple case, different directories have different
// sets of files
storage = mockStorageWithEdits(
"[1,100]|[101,200]",
"[1,100]|[201,300]|[301,400]"); // nothing starting at 101
log = getFSEditLog(storage);
log.initJournalsForWrite();
assertEquals("[[1,100], [101,200], [201,300], [301,400]]" +
" CommittedTxId: 400", log.getEditLogManifest(1).toString());
// Case where one directory has an earlier finalized log, followed
// by a gap. The returned manifest should start after the gap.
storage = mockStorageWithEdits(
"[1,100]|[301,400]", // gap from 101 to 300
"[301,400]|[401,500]");
log = getFSEditLog(storage);
log.initJournalsForWrite();
assertEquals("[[301,400], [401,500]] CommittedTxId: 500",
log.getEditLogManifest(1).toString());
// Case where different directories have different length logs
// starting at the same txid - should pick the longer one
storage = mockStorageWithEdits(
"[1,100]|[101,150]", // short log at 101
"[1,50]|[101,200]"); // short log at 1
log = getFSEditLog(storage);
log.initJournalsForWrite();
assertEquals("[[1,100], [101,200]] CommittedTxId: 200",
log.getEditLogManifest(1).toString());
assertEquals("[[101,200]] CommittedTxId: 200",
log.getEditLogManifest(101).toString());
// Case where the first storage has an inprogress while
// the second has finalised that file (i.e. the first failed
// recently)
storage = mockStorageWithEdits(
"[1,100]|[101,]",
"[1,100]|[101,200]");
log = getFSEditLog(storage);
log.initJournalsForWrite();
assertEquals("[[1,100], [101,200]] CommittedTxId: 200",
log.getEditLogManifest(1).toString());
assertEquals("[[101,200]] CommittedTxId: 200",
log.getEditLogManifest(101).toString());
}
/**
* Create a mock NNStorage object with several directories, each directory
* holding edit logs according to a specification. Each directory
* is specified by a pipe-separated string. For example:
* <code>[1,100]|[101,200]</code> specifies a directory which
* includes two finalized segments, one from 1-100, and one from 101-200.
* The syntax <code>[1,]</code> specifies an in-progress log starting at
* txid 1.
*/
private NNStorage mockStorageWithEdits(String... editsDirSpecs) throws IOException {
List<StorageDirectory> sds = Lists.newArrayList();
List<URI> uris = Lists.newArrayList();
NNStorage storage = Mockito.mock(NNStorage.class);
for (String dirSpec : editsDirSpecs) {
List<String> files = Lists.newArrayList();
String[] logSpecs = dirSpec.split("\\|");
for (String logSpec : logSpecs) {
Matcher m = Pattern.compile("\\[(\\d+),(\\d+)?\\]").matcher(logSpec);
assertTrue("bad spec: " + logSpec, m.matches());
if (m.group(2) == null) {
files.add(NNStorage.getInProgressEditsFileName(
Long.parseLong(m.group(1))));
} else {
files.add(NNStorage.getFinalizedEditsFileName(
Long.parseLong(m.group(1)),
Long.parseLong(m.group(2))));
}
}
StorageDirectory sd = FSImageTestUtil.mockStorageDirectory(
NameNodeDirType.EDITS, false,
files.toArray(new String[0]));
sds.add(sd);
URI u = URI.create("file:///storage"+ Math.random());
Mockito.doReturn(sd).when(storage).getStorageDirectory(u);
uris.add(u);
}
Mockito.doReturn(sds).when(storage).dirIterable(NameNodeDirType.EDITS);
Mockito.doReturn(uris).when(storage).getEditsDirectories();
return storage;
}
/**
* Specification for a failure during #setupEdits
*/
static class AbortSpec {
final int roll;
final int logindex;
/**
* Construct the failure specification.
* @param roll number to fail after. e.g. 1 to fail after the first roll
* @param logindex index of journal to fail.
*/
AbortSpec(int roll, int logindex) {
this.roll = roll;
this.logindex = logindex;
}
}
final static int TXNS_PER_ROLL = 10;
final static int TXNS_PER_FAIL = 2;
/**
* Set up directories for tests.
*
* Each rolled file is 10 txns long.
* A failed file is 2 txns long.
*
* @param editUris directories to create edit logs in
* @param numrolls number of times to roll the edit log during setup
* @param closeOnFinish whether to close the edit log after setup
* @param abortAtRolls Specifications for when to fail, see AbortSpec
*/
public static NNStorage setupEdits(List<URI> editUris, int numrolls,
boolean closeOnFinish, AbortSpec... abortAtRolls) throws IOException {
List<AbortSpec> aborts = new ArrayList<AbortSpec>(Arrays.asList(abortAtRolls));
NNStorage storage = new NNStorage(getConf(),
Collections.<URI>emptyList(),
editUris);
storage.format(new NamespaceInfo());
FSEditLog editlog = getFSEditLog(storage);
// open the edit log and add two transactions
// logGenerationStamp is used, simply because it doesn't
// require complex arguments.
editlog.initJournalsForWrite();
editlog.openForWrite(NameNodeLayoutVersion.CURRENT_LAYOUT_VERSION);
for (int i = 2; i < TXNS_PER_ROLL; i++) {
editlog.logGenerationStamp((long) 0);
}
editlog.logSync();
// Go into edit log rolling loop.
// On each roll, the abortAtRolls abort specs are
// checked to see if an abort is required. If so the
// the specified journal is aborted. It will be brought
// back into rotation automatically by rollEditLog
for (int i = 0; i < numrolls; i++) {
editlog.rollEditLog(NameNodeLayoutVersion.CURRENT_LAYOUT_VERSION);
editlog.logGenerationStamp((long) i);
editlog.logSync();
while (aborts.size() > 0
&& aborts.get(0).roll == (i+1)) {
AbortSpec spec = aborts.remove(0);
editlog.getJournals().get(spec.logindex).abort();
}
for (int j = 3; j < TXNS_PER_ROLL; j++) {
editlog.logGenerationStamp((long) i);
}
editlog.logSync();
}
if (closeOnFinish) {
editlog.close();
}
FSImageTestUtil.logStorageContents(LOG, storage);
return storage;
}
/**
* Set up directories for tests.
*
* Each rolled file is 10 txns long.
* A failed file is 2 txns long.
*
* @param editUris directories to create edit logs in
* @param numrolls number of times to roll the edit log during setup
* @param abortAtRolls Specifications for when to fail, see AbortSpec
*/
public static NNStorage setupEdits(List<URI> editUris, int numrolls,
AbortSpec... abortAtRolls) throws IOException {
return setupEdits(editUris, numrolls, true, abortAtRolls);
}
/**
* Test loading an editlog which has had both its storage fail
* on alternating rolls. Two edit log directories are created.
* The first one fails on odd rolls, the second on even. Test
* that we are able to load the entire editlog regardless.
*/
@Test
public void testAlternatingJournalFailure() throws IOException {
File f1 = new File(TEST_DIR + "/alternatingjournaltest0");
File f2 = new File(TEST_DIR + "/alternatingjournaltest1");
List<URI> editUris = ImmutableList.of(f1.toURI(), f2.toURI());
NNStorage storage = setupEdits(editUris, 10,
new AbortSpec(1, 0),
new AbortSpec(2, 1),
new AbortSpec(3, 0),
new AbortSpec(4, 1),
new AbortSpec(5, 0),
new AbortSpec(6, 1),
new AbortSpec(7, 0),
new AbortSpec(8, 1),
new AbortSpec(9, 0),
new AbortSpec(10, 1));
long totaltxnread = 0;
FSEditLog editlog = getFSEditLog(storage);
editlog.initJournalsForWrite();
long startTxId = 1;
Iterable<EditLogInputStream> editStreams = editlog.selectInputStreams(startTxId,
TXNS_PER_ROLL*11);
for (EditLogInputStream edits : editStreams) {
FSEditLogLoader.EditLogValidation val =
FSEditLogLoader.scanEditLog(edits, Long.MAX_VALUE);
long read = (val.getEndTxId() - edits.getFirstTxId()) + 1;
LOG.info("Loading edits " + edits + " read " + read);
assertEquals(startTxId, edits.getFirstTxId());
startTxId += read;
totaltxnread += read;
}
editlog.close();
storage.close();
assertEquals(TXNS_PER_ROLL*11, totaltxnread);
}
/**
* Test loading an editlog with gaps. A single editlog directory
* is set up. On of the edit log files is deleted. This should
* fail when selecting the input streams as it will not be able
* to select enough streams to load up to 4*TXNS_PER_ROLL.
* There should be 4*TXNS_PER_ROLL transactions as we rolled 3
* times.
*/
@Test
public void testLoadingWithGaps() throws IOException {
File f1 = new File(TEST_DIR + "/gaptest0");
List<URI> editUris = ImmutableList.of(f1.toURI());
NNStorage storage = setupEdits(editUris, 3);
final long startGapTxId = 1*TXNS_PER_ROLL + 1;
final long endGapTxId = 2*TXNS_PER_ROLL;
File[] files = new File(f1, "current").listFiles(new FilenameFilter() {
@Override
public boolean accept(File dir, String name) {
if (name.startsWith(NNStorage.getFinalizedEditsFileName(startGapTxId,
endGapTxId))) {
return true;
}
return false;
}
});
assertEquals(1, files.length);
assertTrue(files[0].delete());
FSEditLog editlog = getFSEditLog(storage);
editlog.initJournalsForWrite();
long startTxId = 1;
try {
editlog.selectInputStreams(startTxId, 4*TXNS_PER_ROLL);
fail("Should have thrown exception");
} catch (IOException ioe) {
GenericTestUtils.assertExceptionContains(
"Gap in transactions. Expected to be able to read up until " +
"at least txid 40 but unable to find any edit logs containing " +
"txid 11", ioe);
}
}
/**
* Test that we can read from a byte stream without crashing.
*
*/
static void validateNoCrash(byte garbage[]) throws IOException {
final File TEST_LOG_NAME = new File(TEST_DIR, "test_edit_log");
EditLogFileOutputStream elfos = null;
EditLogFileInputStream elfis = null;
try {
elfos = new EditLogFileOutputStream(getConf(), TEST_LOG_NAME, 0);
elfos.create(NameNodeLayoutVersion.CURRENT_LAYOUT_VERSION);
elfos.writeRaw(garbage, 0, garbage.length);
elfos.setReadyToFlush();
elfos.flushAndSync(true);
elfos.close();
elfos = null;
elfis = new EditLogFileInputStream(TEST_LOG_NAME);
// verify that we can read everything without killing the JVM or
// throwing an exception other than IOException
try {
while (true) {
FSEditLogOp op = elfis.readOp();
if (op == null)
break;
}
} catch (IOException e) {
} catch (Throwable t) {
fail("Caught non-IOException throwable " +
StringUtils.stringifyException(t));
}
} finally {
if ((elfos != null) && (elfos.isOpen()))
elfos.close();
if (elfis != null)
elfis.close();
}
}
static byte[][] invalidSequenecs = null;
/**
* "Fuzz" test for the edit log.
*
* This tests that we can read random garbage from the edit log without
* crashing the JVM or throwing an unchecked exception.
*/
@Test
public void testFuzzSequences() throws IOException {
final int MAX_GARBAGE_LENGTH = 512;
final int MAX_INVALID_SEQ = 5000;
// The seed to use for our random number generator. When given the same
// seed, Java.util.Random will always produce the same sequence of values.
// This is important because it means that the test is deterministic and
// repeatable on any machine.
final int RANDOM_SEED = 123;
Random r = new Random(RANDOM_SEED);
for (int i = 0; i < MAX_INVALID_SEQ; i++) {
byte[] garbage = new byte[r.nextInt(MAX_GARBAGE_LENGTH)];
r.nextBytes(garbage);
validateNoCrash(garbage);
}
}
private static long readAllEdits(Collection<EditLogInputStream> streams,
long startTxId) throws IOException {
FSEditLogOp op;
long nextTxId = startTxId;
long numTx = 0;
for (EditLogInputStream s : streams) {
while (true) {
op = s.readOp();
if (op == null)
break;
if (op.getTransactionId() != nextTxId) {
throw new IOException("out of order transaction ID! expected " +
nextTxId + " but got " + op.getTransactionId() + " when " +
"reading " + s.getName());
}
numTx++;
nextTxId = op.getTransactionId() + 1;
}
}
return numTx;
}
/**
* Test edit log failover. If a single edit log is missing, other
* edits logs should be used instead.
*/
@Test
public void testEditLogFailOverFromMissing() throws IOException {
File f1 = new File(TEST_DIR + "/failover0");
File f2 = new File(TEST_DIR + "/failover1");
List<URI> editUris = ImmutableList.of(f1.toURI(), f2.toURI());
NNStorage storage = setupEdits(editUris, 3);
final long startErrorTxId = 1*TXNS_PER_ROLL + 1;
final long endErrorTxId = 2*TXNS_PER_ROLL;
File[] files = new File(f1, "current").listFiles(new FilenameFilter() {
@Override
public boolean accept(File dir, String name) {
if (name.startsWith(NNStorage.getFinalizedEditsFileName(startErrorTxId,
endErrorTxId))) {
return true;
}
return false;
}
});
assertEquals(1, files.length);
assertTrue(files[0].delete());
FSEditLog editlog = getFSEditLog(storage);
editlog.initJournalsForWrite();
long startTxId = 1;
Collection<EditLogInputStream> streams = null;
try {
streams = editlog.selectInputStreams(startTxId, 4*TXNS_PER_ROLL);
readAllEdits(streams, startTxId);
} catch (IOException e) {
LOG.error("edit log failover didn't work", e);
fail("Edit log failover didn't work");
} finally {
IOUtils.cleanup(null, streams.toArray(new EditLogInputStream[0]));
}
}
/**
* Test edit log failover from a corrupt edit log
*/
@Test
public void testEditLogFailOverFromCorrupt() throws IOException {
File f1 = new File(TEST_DIR + "/failover0");
File f2 = new File(TEST_DIR + "/failover1");
List<URI> editUris = ImmutableList.of(f1.toURI(), f2.toURI());
NNStorage storage = setupEdits(editUris, 3);
final long startErrorTxId = 1*TXNS_PER_ROLL + 1;
final long endErrorTxId = 2*TXNS_PER_ROLL;
File[] files = new File(f1, "current").listFiles(new FilenameFilter() {
@Override
public boolean accept(File dir, String name) {
if (name.startsWith(NNStorage.getFinalizedEditsFileName(startErrorTxId,
endErrorTxId))) {
return true;
}
return false;
}
});
assertEquals(1, files.length);
long fileLen = files[0].length();
LOG.debug("Corrupting Log File: " + files[0] + " len: " + fileLen);
RandomAccessFile rwf = new RandomAccessFile(files[0], "rw");
rwf.seek(fileLen-4); // seek to checksum bytes
int b = rwf.readInt();
rwf.seek(fileLen-4);
rwf.writeInt(b+1);
rwf.close();
FSEditLog editlog = getFSEditLog(storage);
editlog.initJournalsForWrite();
long startTxId = 1;
Collection<EditLogInputStream> streams = null;
try {
streams = editlog.selectInputStreams(startTxId, 4*TXNS_PER_ROLL);
readAllEdits(streams, startTxId);
} catch (IOException e) {
LOG.error("edit log failover didn't work", e);
fail("Edit log failover didn't work");
} finally {
IOUtils.cleanup(null, streams.toArray(new EditLogInputStream[0]));
}
}
/**
* Test creating a directory with lots and lots of edit log segments
*/
@Test
public void testManyEditLogSegments() throws IOException {
final int NUM_EDIT_LOG_ROLLS = 1000;
// start a cluster
Configuration conf = getConf();
MiniDFSCluster cluster = null;
FileSystem fileSys = null;
try {
cluster = new MiniDFSCluster.Builder(conf).numDataNodes(NUM_DATA_NODES).build();
cluster.waitActive();
fileSys = cluster.getFileSystem();
final FSNamesystem namesystem = cluster.getNamesystem();
FSImage fsimage = namesystem.getFSImage();
final FSEditLog editLog = fsimage.getEditLog();
for (int i = 0; i < NUM_EDIT_LOG_ROLLS; i++){
editLog.logSetReplication("fakefile" + i, (short)(i % 3));
assertExistsInStorageDirs(
cluster, NameNodeDirType.EDITS,
NNStorage.getInProgressEditsFileName((i * 3) + 1));
editLog.logSync();
editLog.rollEditLog(NameNodeLayoutVersion.CURRENT_LAYOUT_VERSION);
assertExistsInStorageDirs(
cluster, NameNodeDirType.EDITS,
NNStorage.getFinalizedEditsFileName((i * 3) + 1, (i * 3) + 3));
}
editLog.close();
} finally {
if(fileSys != null) fileSys.close();
if(cluster != null) cluster.shutdown();
}
// How long does it take to read through all these edit logs?
long startTime = Time.now();
try {
cluster = new MiniDFSCluster.Builder(conf).
numDataNodes(NUM_DATA_NODES).build();
cluster.waitActive();
} finally {
if (cluster != null) {
cluster.shutdown();
}
}
long endTime = Time.now();
double delta = ((float)(endTime - startTime)) / 1000.0;
LOG.info(String.format("loaded %d edit log segments in %.2f seconds",
NUM_EDIT_LOG_ROLLS, delta));
}
/**
* Edit log op instances are cached internally using thread-local storage.
* This test checks that the cached instances are reset in between different
* transactions processed on the same thread, so that we don't accidentally
* apply incorrect attributes to an inode.
*
* @throws IOException if there is an I/O error
*/
@Test
public void testResetThreadLocalCachedOps() throws IOException {
Configuration conf = new HdfsConfiguration();
conf.setBoolean(DFSConfigKeys.DFS_NAMENODE_ACLS_ENABLED_KEY, true);
// Set single handler thread, so all transactions hit same thread-local ops.
conf.setInt(DFSConfigKeys.DFS_NAMENODE_HANDLER_COUNT_KEY, 1);
MiniDFSCluster cluster = null;
FileSystem fileSys = null;
try {
cluster = new MiniDFSCluster.Builder(conf).numDataNodes(1).build();
cluster.waitActive();
fileSys = cluster.getFileSystem();
// Create /dir1 with a default ACL.
Path dir1 = new Path("/dir1");
fileSys.mkdirs(dir1);
List<AclEntry> aclSpec = Lists.newArrayList(
aclEntry(DEFAULT, USER, "foo", READ_EXECUTE));
fileSys.modifyAclEntries(dir1, aclSpec);
// /dir1/dir2 is expected to clone the default ACL.
Path dir2 = new Path("/dir1/dir2");
fileSys.mkdirs(dir2);
// /dir1/file1 is expected to clone the default ACL.
Path file1 = new Path("/dir1/file1");
fileSys.create(file1).close();
// /dir3 is not a child of /dir1, so must not clone the default ACL.
Path dir3 = new Path("/dir3");
fileSys.mkdirs(dir3);
// /file2 is not a child of /dir1, so must not clone the default ACL.
Path file2 = new Path("/file2");
fileSys.create(file2).close();
// Restart and assert the above stated expectations.
IOUtils.cleanup(LOG, fileSys);
cluster.restartNameNode();
fileSys = cluster.getFileSystem();
assertFalse(fileSys.getAclStatus(dir1).getEntries().isEmpty());
assertFalse(fileSys.getAclStatus(dir2).getEntries().isEmpty());
assertFalse(fileSys.getAclStatus(file1).getEntries().isEmpty());
assertTrue(fileSys.getAclStatus(dir3).getEntries().isEmpty());
assertTrue(fileSys.getAclStatus(file2).getEntries().isEmpty());
} finally {
IOUtils.cleanup(LOG, fileSys);
if (cluster != null) {
cluster.shutdown();
}
}
}
class TestAppender extends AppenderSkeleton {
private final List<LoggingEvent> log = new ArrayList<>();
@Override
public boolean requiresLayout() {
return false;
}
@Override
protected void append(final LoggingEvent loggingEvent) {
log.add(loggingEvent);
}
@Override
public void close() {
}
public List<LoggingEvent> getLog() {
return new ArrayList<>(log);
}
}
/**
*
* @throws Exception
*/
@Test
public void testReadActivelyUpdatedLog() throws Exception {
final TestAppender appender = new TestAppender();
LogManager.getRootLogger().addAppender(appender);
Configuration conf = new HdfsConfiguration();
conf.setBoolean(DFSConfigKeys.DFS_NAMENODE_ACLS_ENABLED_KEY, true);
// Set single handler thread, so all transactions hit same thread-local ops.
conf.setInt(DFSConfigKeys.DFS_NAMENODE_HANDLER_COUNT_KEY, 1);
MiniDFSCluster cluster = null;
try {
cluster = new MiniDFSCluster.Builder(conf).numDataNodes(1).build();
cluster.waitActive();
FSImage fsimage = cluster.getNamesystem().getFSImage();
StorageDirectory sd = fsimage.getStorage().getStorageDir(0);
final DistributedFileSystem fileSys = cluster.getFileSystem();
DFSInotifyEventInputStream events = fileSys.getInotifyEventStream();
fileSys.mkdirs(new Path("/test"));
fileSys.mkdirs(new Path("/test/dir1"));
fileSys.delete(new Path("/test/dir1"), true);
fsimage.getEditLog().logSync();
fileSys.mkdirs(new Path("/test/dir2"));
final File inProgressEdit = NNStorage.getInProgressEditsFile(sd, 1);
assertTrue(inProgressEdit.exists());
EditLogFileInputStream elis = new EditLogFileInputStream(inProgressEdit);
FSEditLogOp op;
long pos = 0;
while (true) {
op = elis.readOp();
if (op != null && op.opCode != FSEditLogOpCodes.OP_INVALID) {
pos = elis.getPosition();
} else {
break;
}
}
elis.close();
assertTrue(pos > 0);
RandomAccessFile rwf = new RandomAccessFile(inProgressEdit, "rw");
rwf.seek(pos);
assertEquals(rwf.readByte(), (byte) -1);
rwf.seek(pos + 1);
rwf.writeByte(2);
rwf.close();
events.poll();
String pattern = "Caught exception after reading (.*) ops";
Pattern r = Pattern.compile(pattern);
final List<LoggingEvent> log = appender.getLog();
for (LoggingEvent event : log) {
Matcher m = r.matcher(event.getRenderedMessage());
if (m.find()) {
fail("Should not try to read past latest syned edit log op");
}
}
} finally {
if (cluster != null) {
cluster.shutdown();
}
LogManager.getRootLogger().removeAppender(appender);
}
}
}
| dennishuo/hadoop | hadoop-hdfs-project/hadoop-hdfs/src/test/java/org/apache/hadoop/hdfs/server/namenode/TestEditLog.java | Java | apache-2.0 | 59,670 |
package cvc3;
import java.util.*;
import java.io.*;
/** Wrapper for a c++ object as a java Object.
see README for details on garbage collection,
i.e. interplay of delete, finalize, and EmbeddedManager to destruct
the embedded c++ object. */
public abstract class Embedded {
// load jni c++ library
static {
System.loadLibrary("cvc3jni");
/*
// for debugging: stop here by waiting for a key press,
// and attach c++ debugger
System.out.println("Loadded cvc3jni");
try {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
br.readLine();
} catch (IOException ioe) {
}
*/
}
/// Attributes
// embedded object
protected Object d_embedded;
// embedded object manager
private final EmbeddedManager d_embeddedManager;
/// Constructor
// initialize with embedded object and EmbeddedManager
// if EmbeddedManager is null then delete must be called before
// Embedded is garbage collected
protected Embedded(Object Embedded, EmbeddedManager embeddedManager) {
//System.out.println("Create: Embedded");
assert(Embedded != null);
d_embedded = Embedded;
d_embeddedManager = embeddedManager;
}
// access to embedded c++ object
public synchronized Object embedded() {
return d_embedded;
}
// access to EmbeddedManager (might be null if none used)
public EmbeddedManager embeddedManager() {
return d_embeddedManager;
}
// check if already destructed
// (or queued for destruction in embeddedManager)
public synchronized boolean isDeleted() {
return (d_embedded == null);
}
// delete embedded object or enqueue it for deletion
public synchronized void delete() throws Cvc3Exception {
if (isDeleted()) return;
// no embedded manager, so should be in main thread:
// destruct right away
if (d_embeddedManager == null) {
EmbeddedManager.jniDelete(d_embedded);
}
// could be in finalizer, so queue in embeddedManager;
// unless the embeddedManager is already deleted,
// then its (and this') ValidityChecker has been delete.
// assuming this is an Expr or a Theorem it's embedded object
// has then already been deleted as well.
else {
synchronized(d_embeddedManager) {
if (!d_embeddedManager.isDeleted()) {
d_embeddedManager.register(this);
}
}
}
d_embedded = null;
}
// ensure that delete is called if finalization occurs
public void finalize() throws Throwable {
try {
// no embeddedManager, so deleted should have been called
if (d_embeddedManager == null) {
if (d_embedded != null) {
assert(false);
// System.out.println("Embedded.Finalizer: should never be called");
throw new Error("Embedded.Finalizer: should never be called");
}
}
else if (!d_embeddedManager.isDeleted()) {
delete();
}
} finally {
super.finalize();
}
}
}
| ehsan/js-symbolic-executor | cvc3/java/src/cvc3/Embedded.java | Java | apache-2.0 | 2,909 |
/*
* Copyright (c) 1997, 2008, Oracle and/or its affiliates. All rights reserved.
* ORACLE PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*/
package com.sun.java.swing.plaf.windows;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import java.lang.ref.*;
import java.util.*;
import javax.swing.plaf.basic.*;
import javax.swing.*;
import javax.swing.plaf.ComponentUI;
import static com.sun.java.swing.plaf.windows.TMSchema.*;
import static com.sun.java.swing.plaf.windows.XPStyle.Skin;
/**
* Windows rendition of the component.
* <p>
* <strong>Warning:</strong>
* Serialized objects of this class will not be compatible with
* future Swing releases. The current serialization support is appropriate
* for short term storage or RMI between applications running the same
* version of Swing. A future release of Swing will provide support for
* long term persistence.
*/
public class WindowsScrollBarUI extends BasicScrollBarUI {
private Grid thumbGrid;
private Grid highlightGrid;
private Dimension horizontalThumbSize;
private Dimension verticalThumbSize;
/**
* Creates a UI for a JScrollBar.
*
* @param c the text field
* @return the UI
*/
public static ComponentUI createUI(JComponent c) {
return new WindowsScrollBarUI();
}
protected void installDefaults() {
super.installDefaults();
XPStyle xp = XPStyle.getXP();
if (xp != null) {
scrollbar.setBorder(null);
horizontalThumbSize = getSize(scrollbar, xp, Part.SBP_THUMBBTNHORZ);
verticalThumbSize = getSize(scrollbar, xp, Part.SBP_THUMBBTNVERT);
} else {
horizontalThumbSize = null;
verticalThumbSize = null;
}
}
private static Dimension getSize(Component component, XPStyle xp, Part part) {
Skin skin = xp.getSkin(component, part);
return new Dimension(skin.getWidth(), skin.getHeight());
}
@Override
protected Dimension getMinimumThumbSize() {
if ((horizontalThumbSize == null) || (verticalThumbSize == null)) {
return super.getMinimumThumbSize();
}
return JScrollBar.HORIZONTAL == scrollbar.getOrientation()
? horizontalThumbSize
: verticalThumbSize;
}
public void uninstallUI(JComponent c) {
super.uninstallUI(c);
thumbGrid = highlightGrid = null;
}
protected void configureScrollBarColors() {
super.configureScrollBarColors();
Color color = UIManager.getColor("ScrollBar.trackForeground");
if (color != null && trackColor != null) {
thumbGrid = Grid.getGrid(color, trackColor);
}
color = UIManager.getColor("ScrollBar.trackHighlightForeground");
if (color != null && trackHighlightColor != null) {
highlightGrid = Grid.getGrid(color, trackHighlightColor);
}
}
protected JButton createDecreaseButton(int orientation) {
return new WindowsArrowButton(orientation,
UIManager.getColor("ScrollBar.thumb"),
UIManager.getColor("ScrollBar.thumbShadow"),
UIManager.getColor("ScrollBar.thumbDarkShadow"),
UIManager.getColor("ScrollBar.thumbHighlight"));
}
protected JButton createIncreaseButton(int orientation) {
return new WindowsArrowButton(orientation,
UIManager.getColor("ScrollBar.thumb"),
UIManager.getColor("ScrollBar.thumbShadow"),
UIManager.getColor("ScrollBar.thumbDarkShadow"),
UIManager.getColor("ScrollBar.thumbHighlight"));
}
/**
* {@inheritDoc}
* @since 1.6
*/
@Override
protected ArrowButtonListener createArrowButtonListener(){
// we need to repaint the entire scrollbar because state change for each
// button causes a state change for the thumb and other button on Vista
if(XPStyle.isVista()) {
return new ArrowButtonListener() {
public void mouseEntered(MouseEvent evt) {
repaint();
super.mouseEntered(evt);
}
public void mouseExited(MouseEvent evt) {
repaint();
super.mouseExited(evt);
}
private void repaint() {
scrollbar.repaint();
}
};
} else {
return super.createArrowButtonListener();
}
}
protected void paintTrack(Graphics g, JComponent c, Rectangle trackBounds){
boolean v = (scrollbar.getOrientation() == JScrollBar.VERTICAL);
XPStyle xp = XPStyle.getXP();
if (xp != null) {
JScrollBar sb = (JScrollBar)c;
State state = State.NORMAL;
// Pending: Implement rollover (hot) and pressed
if (!sb.isEnabled()) {
state = State.DISABLED;
}
Part part = v ? Part.SBP_LOWERTRACKVERT : Part.SBP_LOWERTRACKHORZ;
xp.getSkin(sb, part).paintSkin(g, trackBounds, state);
} else if (thumbGrid == null) {
super.paintTrack(g, c, trackBounds);
}
else {
thumbGrid.paint(g, trackBounds.x, trackBounds.y, trackBounds.width,
trackBounds.height);
if (trackHighlight == DECREASE_HIGHLIGHT) {
paintDecreaseHighlight(g);
}
else if (trackHighlight == INCREASE_HIGHLIGHT) {
paintIncreaseHighlight(g);
}
}
}
protected void paintThumb(Graphics g, JComponent c, Rectangle thumbBounds) {
boolean v = (scrollbar.getOrientation() == JScrollBar.VERTICAL);
XPStyle xp = XPStyle.getXP();
if (xp != null) {
JScrollBar sb = (JScrollBar)c;
State state = State.NORMAL;
if (!sb.isEnabled()) {
state = State.DISABLED;
} else if (isDragging) {
state = State.PRESSED;
} else if (isThumbRollover()) {
state = State.HOT;
} else if (XPStyle.isVista()) {
if ((incrButton != null && incrButton.getModel().isRollover()) ||
(decrButton != null && decrButton.getModel().isRollover())) {
state = State.HOVER;
}
}
// Paint thumb
Part thumbPart = v ? Part.SBP_THUMBBTNVERT : Part.SBP_THUMBBTNHORZ;
xp.getSkin(sb, thumbPart).paintSkin(g, thumbBounds, state);
// Paint gripper
Part gripperPart = v ? Part.SBP_GRIPPERVERT : Part.SBP_GRIPPERHORZ;
Skin skin = xp.getSkin(sb, gripperPart);
Insets gripperInsets = xp.getMargin(c, thumbPart, null, Prop.CONTENTMARGINS);
if (gripperInsets == null ||
(v && (thumbBounds.height - gripperInsets.top -
gripperInsets.bottom >= skin.getHeight())) ||
(!v && (thumbBounds.width - gripperInsets.left -
gripperInsets.right >= skin.getWidth()))) {
skin.paintSkin(g,
thumbBounds.x + (thumbBounds.width - skin.getWidth()) / 2,
thumbBounds.y + (thumbBounds.height - skin.getHeight()) / 2,
skin.getWidth(), skin.getHeight(), state);
}
} else {
super.paintThumb(g, c, thumbBounds);
}
}
protected void paintDecreaseHighlight(Graphics g) {
if (highlightGrid == null) {
super.paintDecreaseHighlight(g);
}
else {
Insets insets = scrollbar.getInsets();
Rectangle thumbR = getThumbBounds();
int x, y, w, h;
if (scrollbar.getOrientation() == JScrollBar.VERTICAL) {
x = insets.left;
y = decrButton.getY() + decrButton.getHeight();
w = scrollbar.getWidth() - (insets.left + insets.right);
h = thumbR.y - y;
}
else {
x = decrButton.getX() + decrButton.getHeight();
y = insets.top;
w = thumbR.x - x;
h = scrollbar.getHeight() - (insets.top + insets.bottom);
}
highlightGrid.paint(g, x, y, w, h);
}
}
protected void paintIncreaseHighlight(Graphics g) {
if (highlightGrid == null) {
super.paintDecreaseHighlight(g);
}
else {
Insets insets = scrollbar.getInsets();
Rectangle thumbR = getThumbBounds();
int x, y, w, h;
if (scrollbar.getOrientation() == JScrollBar.VERTICAL) {
x = insets.left;
y = thumbR.y + thumbR.height;
w = scrollbar.getWidth() - (insets.left + insets.right);
h = incrButton.getY() - y;
}
else {
x = thumbR.x + thumbR.width;
y = insets.top;
w = incrButton.getX() - x;
h = scrollbar.getHeight() - (insets.top + insets.bottom);
}
highlightGrid.paint(g, x, y, w, h);
}
}
/**
* {@inheritDoc}
* @since 1.6
*/
@Override
protected void setThumbRollover(boolean active) {
boolean old = isThumbRollover();
super.setThumbRollover(active);
// we need to repaint the entire scrollbar because state change for thumb
// causes state change for incr and decr buttons on Vista
if(XPStyle.isVista() && active != old) {
scrollbar.repaint();
}
}
/**
* WindowsArrowButton is used for the buttons to position the
* document up/down. It differs from BasicArrowButton in that the
* preferred size is always a square.
*/
private class WindowsArrowButton extends BasicArrowButton {
public WindowsArrowButton(int direction, Color background, Color shadow,
Color darkShadow, Color highlight) {
super(direction, background, shadow, darkShadow, highlight);
}
public WindowsArrowButton(int direction) {
super(direction);
}
public void paint(Graphics g) {
XPStyle xp = XPStyle.getXP();
if (xp != null) {
ButtonModel model = getModel();
Skin skin = xp.getSkin(this, Part.SBP_ARROWBTN);
State state = null;
boolean jointRollover = XPStyle.isVista() && (isThumbRollover() ||
(this == incrButton && decrButton.getModel().isRollover()) ||
(this == decrButton && incrButton.getModel().isRollover()));
// normal, rollover, pressed, disabled
if (model.isArmed() && model.isPressed()) {
switch (direction) {
case NORTH: state = State.UPPRESSED; break;
case SOUTH: state = State.DOWNPRESSED; break;
case WEST: state = State.LEFTPRESSED; break;
case EAST: state = State.RIGHTPRESSED; break;
}
} else if (!model.isEnabled()) {
switch (direction) {
case NORTH: state = State.UPDISABLED; break;
case SOUTH: state = State.DOWNDISABLED; break;
case WEST: state = State.LEFTDISABLED; break;
case EAST: state = State.RIGHTDISABLED; break;
}
} else if (model.isRollover() || model.isPressed()) {
switch (direction) {
case NORTH: state = State.UPHOT; break;
case SOUTH: state = State.DOWNHOT; break;
case WEST: state = State.LEFTHOT; break;
case EAST: state = State.RIGHTHOT; break;
}
} else if (jointRollover) {
switch (direction) {
case NORTH: state = State.UPHOVER; break;
case SOUTH: state = State.DOWNHOVER; break;
case WEST: state = State.LEFTHOVER; break;
case EAST: state = State.RIGHTHOVER; break;
}
} else {
switch (direction) {
case NORTH: state = State.UPNORMAL; break;
case SOUTH: state = State.DOWNNORMAL; break;
case WEST: state = State.LEFTNORMAL; break;
case EAST: state = State.RIGHTNORMAL; break;
}
}
skin.paintSkin(g, 0, 0, getWidth(), getHeight(), state);
} else {
super.paint(g);
}
}
public Dimension getPreferredSize() {
int size = 16;
if (scrollbar != null) {
switch (scrollbar.getOrientation()) {
case JScrollBar.VERTICAL:
size = scrollbar.getWidth();
break;
case JScrollBar.HORIZONTAL:
size = scrollbar.getHeight();
break;
}
size = Math.max(size, 5);
}
return new Dimension(size, size);
}
}
/**
* This should be pulled out into its own class if more classes need to
* use it.
* <p>
* Grid is used to draw the track for windows scrollbars. Grids
* are cached in a HashMap, with the key being the rgb components
* of the foreground/background colors. Further the Grid is held through
* a WeakRef so that it can be freed when no longer needed. As the
* Grid is rather expensive to draw, it is drawn in a BufferedImage.
*/
private static class Grid {
private static final int BUFFER_SIZE = 64;
private static HashMap<String, WeakReference<Grid>> map;
private BufferedImage image;
static {
map = new HashMap<String, WeakReference<Grid>>();
}
public static Grid getGrid(Color fg, Color bg) {
String key = fg.getRGB() + " " + bg.getRGB();
WeakReference<Grid> ref = map.get(key);
Grid grid = (ref == null) ? null : ref.get();
if (grid == null) {
grid = new Grid(fg, bg);
map.put(key, new WeakReference<Grid>(grid));
}
return grid;
}
public Grid(Color fg, Color bg) {
int cmap[] = { fg.getRGB(), bg.getRGB() };
IndexColorModel icm = new IndexColorModel(8, 2, cmap, 0, false, -1,
DataBuffer.TYPE_BYTE);
image = new BufferedImage(BUFFER_SIZE, BUFFER_SIZE,
BufferedImage.TYPE_BYTE_INDEXED, icm);
Graphics g = image.getGraphics();
try {
g.setClip(0, 0, BUFFER_SIZE, BUFFER_SIZE);
paintGrid(g, fg, bg);
}
finally {
g.dispose();
}
}
/**
* Paints the grid into the specified Graphics at the specified
* location.
*/
public void paint(Graphics g, int x, int y, int w, int h) {
Rectangle clipRect = g.getClipBounds();
int minX = Math.max(x, clipRect.x);
int minY = Math.max(y, clipRect.y);
int maxX = Math.min(clipRect.x + clipRect.width, x + w);
int maxY = Math.min(clipRect.y + clipRect.height, y + h);
if (maxX <= minX || maxY <= minY) {
return;
}
int xOffset = (minX - x) % 2;
for (int xCounter = minX; xCounter < maxX;
xCounter += BUFFER_SIZE) {
int yOffset = (minY - y) % 2;
int width = Math.min(BUFFER_SIZE - xOffset,
maxX - xCounter);
for (int yCounter = minY; yCounter < maxY;
yCounter += BUFFER_SIZE) {
int height = Math.min(BUFFER_SIZE - yOffset,
maxY - yCounter);
g.drawImage(image, xCounter, yCounter,
xCounter + width, yCounter + height,
xOffset, yOffset,
xOffset + width, yOffset + height, null);
if (yOffset != 0) {
yCounter -= yOffset;
yOffset = 0;
}
}
if (xOffset != 0) {
xCounter -= xOffset;
xOffset = 0;
}
}
}
/**
* Actually renders the grid into the Graphics <code>g</code>.
*/
private void paintGrid(Graphics g, Color fg, Color bg) {
Rectangle clipRect = g.getClipBounds();
g.setColor(bg);
g.fillRect(clipRect.x, clipRect.y, clipRect.width,
clipRect.height);
g.setColor(fg);
g.translate(clipRect.x, clipRect.y);
int width = clipRect.width;
int height = clipRect.height;
int xCounter = clipRect.x % 2;
for (int end = width - height; xCounter < end; xCounter += 2) {
g.drawLine(xCounter, 0, xCounter + height, height);
}
for (int end = width; xCounter < end; xCounter += 2) {
g.drawLine(xCounter, 0, width, width - xCounter);
}
int yCounter = ((clipRect.x % 2) == 0) ? 2 : 1;
for (int end = height - width; yCounter < end; yCounter += 2) {
g.drawLine(0, yCounter, width, yCounter + width);
}
for (int end = height; yCounter < end; yCounter += 2) {
g.drawLine(0, yCounter, height - yCounter, height);
}
g.translate(-clipRect.x, -clipRect.y);
}
}
}
| shun634501730/java_source_cn | src_en/com/sun/java/swing/plaf/windows/WindowsScrollBarUI.java | Java | apache-2.0 | 18,587 |
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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.apache.hadoop.hbase.master.assignment;
import static org.junit.Assert.fail;
import org.apache.hadoop.hbase.HBaseClassTestRule;
import org.apache.hadoop.hbase.HBaseTestingUtil;
import org.apache.hadoop.hbase.testclassification.MasterTests;
import org.apache.hadoop.hbase.testclassification.MediumTests;
import org.junit.After;
import org.junit.Before;
import org.junit.ClassRule;
import org.junit.Test;
import org.junit.experimental.categories.Category;
/**
* Testcase for HBASE-23682.
*/
@Category({ MasterTests.class, MediumTests.class })
public class TestDeadServerMetricRegionChore {
@ClassRule
public static final HBaseClassTestRule CLASS_RULE =
HBaseClassTestRule.forClass(TestDeadServerMetricRegionChore.class);
protected HBaseTestingUtil util;
@Before
public void setUp() throws Exception {
util = new HBaseTestingUtil();
// Disable DeadServerMetricRegionChore
util.getConfiguration()
.setInt(AssignmentManager.DEAD_REGION_METRIC_CHORE_INTERVAL_MSEC_CONF_KEY, -1);
}
@After
public void tearDown() throws Exception {
this.util.shutdownMiniCluster();
}
@Test
public void testDeadServerMetricRegionChore() throws Exception {
try {
this.util.startMiniCluster();
} catch (Exception e) {
fail("Start cluster failed");
}
}
}
| apurtell/hbase | hbase-server/src/test/java/org/apache/hadoop/hbase/master/assignment/TestDeadServerMetricRegionChore.java | Java | apache-2.0 | 2,127 |
/*
* 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 com.facebook.presto.thrift.api.udf;
import com.facebook.drift.annotations.ThriftConstructor;
import com.facebook.drift.annotations.ThriftField;
import com.facebook.drift.annotations.ThriftStruct;
import com.google.common.collect.ImmutableList;
import javax.annotation.Nullable;
import javax.annotation.concurrent.Immutable;
import java.util.List;
import static com.facebook.drift.annotations.ThriftField.Recursiveness.TRUE;
import static com.facebook.drift.annotations.ThriftField.Requiredness.OPTIONAL;
import static java.util.Objects.requireNonNull;
@Immutable
@ThriftStruct
public class UdfExecutionFailureInfo
{
private final String type;
private final String message;
private final UdfExecutionFailureInfo cause;
private final List<UdfExecutionFailureInfo> suppressed;
private final List<String> stack;
@ThriftConstructor
public UdfExecutionFailureInfo(
String type,
String message,
@Nullable UdfExecutionFailureInfo cause,
List<UdfExecutionFailureInfo> suppressed,
List<String> stack)
{
this.type = requireNonNull(type, "type is null");
this.message = requireNonNull(message, "message is null");
this.cause = cause;
this.suppressed = ImmutableList.copyOf(suppressed);
this.stack = ImmutableList.copyOf(stack);
}
@ThriftField(1)
public String getType()
{
return type;
}
@Nullable
@ThriftField(2)
public String getMessage()
{
return message;
}
@Nullable
@ThriftField(value = 3, isRecursive = TRUE, requiredness = OPTIONAL)
public UdfExecutionFailureInfo getCause()
{
return cause;
}
@ThriftField(4)
public List<UdfExecutionFailureInfo> getSuppressed()
{
return suppressed;
}
@ThriftField(5)
public List<String> getStack()
{
return stack;
}
}
| facebook/presto | presto-thrift-api/src/main/java/com/facebook/presto/thrift/api/udf/UdfExecutionFailureInfo.java | Java | apache-2.0 | 2,491 |
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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.apache.storm.mongodb.topology;
import org.apache.storm.spout.SpoutOutputCollector;
import org.apache.storm.task.TopologyContext;
import org.apache.storm.topology.IRichSpout;
import org.apache.storm.topology.OutputFieldsDeclarer;
import org.apache.storm.tuple.Fields;
import org.apache.storm.tuple.Values;
import java.util.Map;
import java.util.Random;
import java.util.UUID;
public class WordSpout implements IRichSpout {
boolean isDistributed;
SpoutOutputCollector collector;
public static final String[] words = new String[] { "apple", "orange", "pineapple", "banana", "watermelon" };
public WordSpout() {
this(true);
}
public WordSpout(boolean isDistributed) {
this.isDistributed = isDistributed;
}
public boolean isDistributed() {
return this.isDistributed;
}
@SuppressWarnings("rawtypes")
public void open(Map conf, TopologyContext context, SpoutOutputCollector collector) {
this.collector = collector;
}
public void close() {
}
public void nextTuple() {
final Random rand = new Random();
final String word = words[rand.nextInt(words.length)];
this.collector.emit(new Values(word), UUID.randomUUID());
Thread.yield();
}
public void ack(Object msgId) {
}
public void fail(Object msgId) {
}
public void declareOutputFields(OutputFieldsDeclarer declarer) {
declarer.declare(new Fields("word"));
}
@Override
public void activate() {
}
@Override
public void deactivate() {
}
@Override
public Map<String, Object> getComponentConfiguration() {
return null;
}
}
| dke-knu/i2am | rdma-based-storm/examples/storm-mongodb-examples/src/main/java/org/apache/storm/mongodb/topology/WordSpout.java | Java | apache-2.0 | 2,496 |
//
// MessagePack for Java
//
// 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.msgpack.core;
import java.math.BigInteger;
/**
* This error is thrown when the user tries to read an integer value
* using a smaller types. For example, calling MessageUnpacker.unpackInt() for an integer value
* that is larger than Integer.MAX_VALUE will cause this exception.
*/
public class MessageIntegerOverflowException
extends MessageTypeException
{
private final BigInteger bigInteger;
public MessageIntegerOverflowException(BigInteger bigInteger)
{
super();
this.bigInteger = bigInteger;
}
public MessageIntegerOverflowException(long value)
{
this(BigInteger.valueOf(value));
}
public MessageIntegerOverflowException(String message, BigInteger bigInteger)
{
super(message);
this.bigInteger = bigInteger;
}
public BigInteger getBigInteger()
{
return bigInteger;
}
@Override
public String getMessage()
{
return bigInteger.toString();
}
}
| jackyglony/msgpack-java | msgpack-core/src/main/java/org/msgpack/core/MessageIntegerOverflowException.java | Java | apache-2.0 | 1,614 |
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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.apache.hadoop.hbase.master.balancer;
import java.util.List;
import java.util.Map;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.HRegionInfo;
import org.apache.hadoop.hbase.MediumTests;
import org.apache.hadoop.hbase.ServerName;
import org.apache.hadoop.hbase.master.LoadBalancer;
import org.apache.hadoop.hbase.master.RegionPlan;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;
/**
* Test the load balancer that is created by default.
*/
@Category(MediumTests.class)
public class TestDefaultLoadBalancer extends BalancerTestBase {
private static final Log LOG = LogFactory.getLog(TestDefaultLoadBalancer.class);
private static LoadBalancer loadBalancer;
@BeforeClass
public static void beforeAllTests() throws Exception {
Configuration conf = HBaseConfiguration.create();
conf.set("hbase.regions.slop", "0");
loadBalancer = new DefaultLoadBalancer();
loadBalancer.setConf(conf);
}
// int[testnum][servernumber] -> numregions
int[][] clusterStateMocks = new int[][] {
// 1 node
new int[] { 0 },
new int[] { 1 },
new int[] { 10 },
// 2 node
new int[] { 0, 0 },
new int[] { 2, 0 },
new int[] { 2, 1 },
new int[] { 2, 2 },
new int[] { 2, 3 },
new int[] { 2, 4 },
new int[] { 1, 1 },
new int[] { 0, 1 },
new int[] { 10, 1 },
new int[] { 14, 1432 },
new int[] { 47, 53 },
// 3 node
new int[] { 0, 1, 2 },
new int[] { 1, 2, 3 },
new int[] { 0, 2, 2 },
new int[] { 0, 3, 0 },
new int[] { 0, 4, 0 },
new int[] { 20, 20, 0 },
// 4 node
new int[] { 0, 1, 2, 3 },
new int[] { 4, 0, 0, 0 },
new int[] { 5, 0, 0, 0 },
new int[] { 6, 6, 0, 0 },
new int[] { 6, 2, 0, 0 },
new int[] { 6, 1, 0, 0 },
new int[] { 6, 0, 0, 0 },
new int[] { 4, 4, 4, 7 },
new int[] { 4, 4, 4, 8 },
new int[] { 0, 0, 0, 7 },
// 5 node
new int[] { 1, 1, 1, 1, 4 },
// more nodes
new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 },
new int[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 10 }, new int[] { 6, 6, 5, 6, 6, 6, 6, 6, 6, 1 },
new int[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 54 }, new int[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 55 },
new int[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 56 }, new int[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 16 },
new int[] { 1, 1, 1, 1, 1, 1, 1, 1, 1, 8 }, new int[] { 1, 1, 1, 1, 1, 1, 1, 1, 1, 9 },
new int[] { 1, 1, 1, 1, 1, 1, 1, 1, 1, 10 }, new int[] { 1, 1, 1, 1, 1, 1, 1, 1, 1, 123 },
new int[] { 1, 1, 1, 1, 1, 1, 1, 1, 1, 155 },
new int[] { 0, 0, 144, 1, 1, 1, 1, 1123, 133, 138, 12, 1444 },
new int[] { 0, 0, 144, 1, 0, 4, 1, 1123, 133, 138, 12, 1444 },
new int[] { 1538, 1392, 1561, 1557, 1535, 1553, 1385, 1542, 1619 } };
/**
* Test the load balancing algorithm.
*
* Invariant is that all servers should be hosting either floor(average) or
* ceiling(average)
*
* @throws Exception
*/
@Test
public void testBalanceCluster() throws Exception {
for (int[] mockCluster : clusterStateMocks) {
Map<ServerName, List<HRegionInfo>> servers = mockClusterServers(mockCluster);
List<ServerAndLoad> list = convertToList(servers);
LOG.info("Mock Cluster : " + printMock(list) + " " + printStats(list));
List<RegionPlan> plans = loadBalancer.balanceCluster(servers);
List<ServerAndLoad> balancedCluster = reconcile(list, plans);
LOG.info("Mock Balance : " + printMock(balancedCluster));
assertClusterAsBalanced(balancedCluster);
for (Map.Entry<ServerName, List<HRegionInfo>> entry : servers.entrySet()) {
returnRegions(entry.getValue());
returnServer(entry.getKey());
}
}
}
}
| daidong/DominoHBase | hbase-server/src/test/java/org/apache/hadoop/hbase/master/balancer/TestDefaultLoadBalancer.java | Java | apache-2.0 | 4,805 |
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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.apache.flink.table.descriptors;
import org.apache.flink.annotation.Internal;
import org.apache.flink.table.api.ValidationException;
import java.util.HashMap;
import java.util.Map;
import java.util.function.Consumer;
/** Validator for {@link FunctionDescriptor}. */
@Internal
public class FunctionDescriptorValidator implements DescriptorValidator {
public static final String FROM = "from";
public static final String FROM_VALUE_CLASS = "class";
public static final String FROM_VALUE_PYTHON = "python";
@Override
public void validate(DescriptorProperties properties) {
Map<String, Consumer<String>> enumValidation = new HashMap<>();
enumValidation.put(
FROM_VALUE_CLASS, s -> new ClassInstanceValidator().validate(properties));
enumValidation.put(
FROM_VALUE_PYTHON, s -> new PythonFunctionValidator().validate(properties));
// check for 'from'
if (properties.containsKey(FROM)) {
properties.validateEnum(FROM, false, enumValidation);
} else {
throw new ValidationException("Could not find 'from' property for function.");
}
}
}
| rmetzger/flink | flink-table/flink-table-common/src/main/java/org/apache/flink/table/descriptors/FunctionDescriptorValidator.java | Java | apache-2.0 | 1,990 |
/*
* Copyright (c) 2005, 2006, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code 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 General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package sun.security.smartcardio;
import java.security.*;
import javax.smartcardio.*;
/**
* Provider object for PC/SC.
*
* @since 1.6
* @author Andreas Sterbenz
*/
public final class SunPCSC extends Provider {
private static final long serialVersionUID = 6168388284028876579L;
public SunPCSC() {
super("SunPCSC", 1.6d, "Sun PC/SC provider");
AccessController.doPrivileged(new PrivilegedAction<Void>() {
public Void run() {
put("TerminalFactory.PC/SC", "sun.security.smartcardio.SunPCSC$Factory");
return null;
}
});
}
public static final class Factory extends TerminalFactorySpi {
public Factory(Object obj) throws PCSCException {
if (obj != null) {
throw new IllegalArgumentException
("SunPCSC factory does not use parameters");
}
// make sure PCSC is available and that we can obtain a context
PCSC.checkAvailable();
PCSCTerminals.initContext();
}
/**
* Returns the available readers.
* This must be a new object for each call.
*/
protected CardTerminals engineTerminals() {
return new PCSCTerminals();
}
}
}
| andreagenso/java2scala | test/J2s/java/openjdk-6-src-b27/jdk/src/share/classes/sun/security/smartcardio/SunPCSC.java | Java | apache-2.0 | 2,515 |
/*
* Copyright 2017 - 2018 Anton Tananaev (anton@traccar.org)
*
* 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.traccar.protocol;
import io.netty.handler.codec.string.StringDecoder;
import io.netty.handler.codec.string.StringEncoder;
import org.traccar.BaseProtocol;
import org.traccar.PipelineBuilder;
import org.traccar.TrackerServer;
public class TmgProtocol extends BaseProtocol {
public TmgProtocol() {
addServer(new TrackerServer(false, getName()) {
@Override
protected void addProtocolHandlers(PipelineBuilder pipeline) {
pipeline.addLast(new TmgFrameDecoder());
pipeline.addLast(new StringEncoder());
pipeline.addLast(new StringDecoder());
pipeline.addLast(new TmgProtocolDecoder(TmgProtocol.this));
}
});
}
}
| tananaev/traccar | src/main/java/org/traccar/protocol/TmgProtocol.java | Java | apache-2.0 | 1,371 |
/* Copyright 2005-2006 Tim Fennell
*
* 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 net.sourceforge.stripes.util;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.regex.Pattern;
/**
* Provides simple utility methods for dealing with HTML.
*
* @author Tim Fennell
*/
public class HtmlUtil {
private static final String FIELD_DELIMITER_STRING = "||";
private static final Pattern FIELD_DELIMITER_PATTERN = Pattern.compile("\\|\\|");
/**
* Replaces special HTML characters from the set {@literal [<, >, ", ', &]} with their HTML
* escape codes. Note that because the escape codes are multi-character that the returned
* String could be longer than the one passed in.
*
* @param fragment a String fragment that might have HTML special characters in it
* @return the fragment with special characters escaped
*/
public static String encode(String fragment) {
// If the input is null, then the output is null
if (fragment == null) return null;
StringBuilder builder = new StringBuilder(fragment.length() + 10); // a little wiggle room
char[] characters = fragment.toCharArray();
// This loop used to also look for and replace single ticks with ' but it
// turns out that it's not strictly necessary since Stripes uses double-quotes
// around all form fields, and stupid IE6 will render ' verbatim instead
// of as a single quote.
for (int i=0; i<characters.length; ++i) {
switch (characters[i]) {
case '<' : builder.append("<"); break;
case '>' : builder.append(">"); break;
case '"' : builder.append("""); break;
case '&' : builder.append("&"); break;
default: builder.append(characters[i]);
}
}
return builder.toString();
}
/**
* One of a pair of methods (the other is splitValues) that is used to combine several
* un-encoded values into a single delimited, encoded value for placement into a
* hidden field.
*
* @param values One or more values which are to be combined
* @return a single HTML-encoded String that contains all the values in such a way that
* they can be converted back into a Collection of Strings with splitValues().
*/
public static String combineValues(Collection<String> values) {
if (values == null || values.size() == 0) {
return "";
}
else {
StringBuilder builder = new StringBuilder(values.size() * 30);
for (String value : values) {
builder.append(value).append(FIELD_DELIMITER_STRING);
}
return encode(builder.toString());
}
}
/**
* Takes in a String produced by combineValues and returns a Collection of values that
* contains the same values as originally supplied to combineValues. Note that the order
* or items in the collection (and indeed the type of Collection used) are not guaranteed
* to be the same.
*
* @param value a String value produced by
* @return a Collection of zero or more Strings
*/
public static Collection<String> splitValues(String value) {
if (value == null || value.length() == 0) {
return Collections.emptyList();
}
else {
String[] splits = FIELD_DELIMITER_PATTERN.split(value);
return Arrays.asList(splits);
}
}
}
| scarcher2/stripes | stripes/src/net/sourceforge/stripes/util/HtmlUtil.java | Java | apache-2.0 | 4,132 |
/*
* Copyright 2017 ThoughtWorks, Inc.
*
* 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 com.thoughtworks.go.agent.common;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import java.io.ByteArrayOutputStream;
import java.io.PrintStream;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.is;
import static org.junit.Assert.assertThat;
public class AgentCLITest {
private ByteArrayOutputStream errorStream;
private AgentCLI agentCLI;
private AgentCLI.SystemExitter exitter;
@Before
public void setUp() throws Exception {
errorStream = new ByteArrayOutputStream();
exitter = new AgentCLI.SystemExitter() {
@Override
public void exit(int status) {
throw new ExitException(status);
}
};
agentCLI = new AgentCLI(new PrintStream(errorStream), exitter);
}
@Test
public void shouldDieIfNoArguments() {
try {
agentCLI.parse();
Assert.fail("Was expecting an exception!");
} catch (ExitException e) {
assertThat(e.getStatus(), is(1));
assertThat(errorStream.toString(), containsString("The following option is required: [-serverUrl]"));
assertThat(errorStream.toString(), containsString("Usage: java -jar agent-bootstrapper.jar"));
}
}
@Test
public void serverURLMustBeAValidURL() throws Exception {
try {
agentCLI.parse("-serverUrl", "foobar");
Assert.fail("Was expecting an exception!");
} catch (ExitException e) {
assertThat(e.getStatus(), is(1));
assertThat(errorStream.toString(), containsString("-serverUrl is not a valid url"));
assertThat(errorStream.toString(), containsString("Usage: java -jar agent-bootstrapper.jar"));
}
}
@Test
public void serverURLMustBeSSL() throws Exception {
try {
agentCLI.parse("-serverUrl", "http://go.example.com:8154/go");
Assert.fail("Was expecting an exception!");
} catch (ExitException e) {
assertThat(e.getStatus(), is(1));
assertThat(errorStream.toString(), containsString("serverUrl must be an HTTPS url and must begin with https://"));
assertThat(errorStream.toString(), containsString("Usage: java -jar agent-bootstrapper.jar"));
}
}
@Test
public void shouldPassIfCorrectArgumentsAreProvided() throws Exception {
AgentBootstrapperArgs agentBootstrapperArgs = agentCLI.parse("-serverUrl", "https://go.example.com:8154/go", "-sslVerificationMode", "NONE");
assertThat(agentBootstrapperArgs.getServerUrl().toString(), is("https://go.example.com:8154/go"));
assertThat(agentBootstrapperArgs.getSslMode(), is(AgentBootstrapperArgs.SslMode.NONE));
}
@Test
public void shouldRaisExceptionWhenInvalidSslModeIsPassed() throws Exception {
try {
agentCLI.parse("-serverUrl", "https://go.example.com:8154/go", "-sslVerificationMode", "FOOBAR");
Assert.fail("Was expecting an exception!");
} catch (ExitException e) {
assertThat(e.getStatus(), is(1));
assertThat(errorStream.toString(), containsString("Invalid value for -sslVerificationMode parameter. Allowed values:[FULL, NONE, NO_VERIFY_HOST]"));
assertThat(errorStream.toString(), containsString("Usage: java -jar agent-bootstrapper.jar"));
}
}
@Test
public void shouldDefaultsTheSslModeToNONEWhenNotSpecified() throws Exception {
AgentBootstrapperArgs agentBootstrapperArgs = agentCLI.parse("-serverUrl", "https://go.example.com/go");
assertThat(agentBootstrapperArgs.getSslMode(), is(AgentBootstrapperArgs.SslMode.NONE));
}
@Test
public void printsHelpAndExitsWith0() throws Exception {
try {
agentCLI.parse("-help");
Assert.fail("Was expecting an exception!");
} catch (ExitException e) {
assertThat(e.getStatus(), is(0));
}
}
class ExitException extends RuntimeException {
private final int status;
public ExitException(int status) {
this.status = status;
}
public int getStatus() {
return status;
}
}
}
| varshavaradarajan/gocd | agent-common/src/test/java/com/thoughtworks/go/agent/common/AgentCLITest.java | Java | apache-2.0 | 4,889 |
package com.fincatto.documentofiscal.nfe400.utils.qrcode20;
import java.math.BigInteger;
import java.nio.charset.Charset;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import org.apache.commons.lang3.StringUtils;
import com.fincatto.documentofiscal.DFAmbiente;
import com.fincatto.documentofiscal.nfe.NFeConfig;
import com.fincatto.documentofiscal.nfe400.classes.nota.NFNota;
/**
* Classe abstrata para a implementação da geração do QRCode 2.0.
*
* Deve ser feita a implementação para emissão normal (1) e para contingência offline (9).
*/
public abstract class NFGeraQRCode20 {
public static final String VERSAO_QRCODE = "2";
protected final NFNota nota;
protected final NFeConfig config;
public NFGeraQRCode20(final NFNota nota, final NFeConfig config) {
this.nota = nota;
this.config = config;
}
/**
* Método responsável pela geração do qrcode.
*
* @return URL para consulta da nota via qrcode20.
* @throws NoSuchAlgorithmException
*/
public abstract String getQRCode() throws NoSuchAlgorithmException;
public String getUrlQRCode(){
String url = this.config.getAmbiente().equals(DFAmbiente.PRODUCAO) ? this.nota.getInfo().getIdentificacao().getUf().getQrCodeProducao() : this.nota.getInfo().getIdentificacao().getUf().getQrCodeHomologacao();
if (StringUtils.isBlank(url)) {
throw new IllegalArgumentException("URL para consulta do QRCode nao informada para uf " + this.nota.getInfo().getIdentificacao().getUf() + "!");
}
if (StringUtils.isBlank(this.config.getCodigoSegurancaContribuinte())) {
throw new IllegalArgumentException("CSC nao informado nas configuracoes!");
}
if ((this.config.getCodigoSegurancaContribuinteID() == null) || (this.config.getCodigoSegurancaContribuinteID() == 0)) {
throw new IllegalArgumentException("IdCSC nao informado nas configuracoes!");
}
return url;
}
public static String createHash(final String campos, final String csc) throws NoSuchAlgorithmException {
return sha1(campos + csc);
}
public static String toHex(final String arg) {
return String.format("%040x", new BigInteger(1, arg.getBytes(Charset.forName("UTF-8"))));
}
public static String sha1(final String input) throws NoSuchAlgorithmException {
final StringBuilder sb = new StringBuilder();
for (final byte element : MessageDigest.getInstance("SHA1").digest(input.getBytes(Charset.forName("UTF-8")))) {
sb.append(Integer.toString((element & 0xff) + 0x100, 16).substring(1));
}
return sb.toString().toUpperCase();
}
public String urlConsultaChaveAcesso(){
return this.config.getAmbiente().equals(DFAmbiente.PRODUCAO) ? this.nota.getInfo().getIdentificacao().getUf().getConsultaChaveAcessoProducao() : this.nota.getInfo().getIdentificacao().getUf().getConsultaChaveAcessoHomologacao();
}
} | fincatto/nfe | src/main/java/com/fincatto/documentofiscal/nfe400/utils/qrcode20/NFGeraQRCode20.java | Java | apache-2.0 | 3,031 |
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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.apache.camel.builder.component.dsl;
import javax.annotation.Generated;
import org.apache.camel.Component;
import org.apache.camel.builder.component.AbstractComponentBuilder;
import org.apache.camel.builder.component.ComponentBuilder;
import org.apache.camel.component.mina.MinaComponent;
/**
* Socket level networking using TCP or UDP with Apache Mina 2.x.
*
* Generated by camel-package-maven-plugin - do not edit this file!
*/
@Generated("org.apache.camel.maven.packaging.ComponentDslMojo")
public interface MinaComponentBuilderFactory {
/**
* Mina (camel-mina)
* Socket level networking using TCP or UDP with Apache Mina 2.x.
*
* Category: networking,tcp,udp
* Since: 2.10
* Maven coordinates: org.apache.camel:camel-mina
*
* @return the dsl builder
*/
static MinaComponentBuilder mina() {
return new MinaComponentBuilderImpl();
}
/**
* Builder for the Mina component.
*/
interface MinaComponentBuilder extends ComponentBuilder<MinaComponent> {
/**
* Whether or not to disconnect(close) from Mina session right after
* use. Can be used for both consumer and producer.
*
* The option is a: <code>boolean</code> type.
*
* Default: false
* Group: common
*
* @param disconnect the value to set
* @return the dsl builder
*/
default MinaComponentBuilder disconnect(boolean disconnect) {
doSetProperty("disconnect", disconnect);
return this;
}
/**
* You can enable the Apache MINA logging filter. Apache MINA uses slf4j
* logging at INFO level to log all input and output.
*
* The option is a: <code>boolean</code> type.
*
* Default: false
* Group: common
*
* @param minaLogger the value to set
* @return the dsl builder
*/
default MinaComponentBuilder minaLogger(boolean minaLogger) {
doSetProperty("minaLogger", minaLogger);
return this;
}
/**
* Setting to set endpoint as one-way or request-response.
*
* The option is a: <code>boolean</code> type.
*
* Default: true
* Group: common
*
* @param sync the value to set
* @return the dsl builder
*/
default MinaComponentBuilder sync(boolean sync) {
doSetProperty("sync", sync);
return this;
}
/**
* You can configure the timeout that specifies how long to wait for a
* response from a remote server. The timeout unit is in milliseconds,
* so 60000 is 60 seconds.
*
* The option is a: <code>long</code> type.
*
* Default: 30000
* Group: common
*
* @param timeout the value to set
* @return the dsl builder
*/
default MinaComponentBuilder timeout(long timeout) {
doSetProperty("timeout", timeout);
return this;
}
/**
* Maximum amount of time it should take to send data to the MINA
* session. Default is 10000 milliseconds.
*
* The option is a: <code>long</code> type.
*
* Default: 10000
* Group: common
*
* @param writeTimeout the value to set
* @return the dsl builder
*/
default MinaComponentBuilder writeTimeout(long writeTimeout) {
doSetProperty("writeTimeout", writeTimeout);
return this;
}
/**
* Allows for bridging the consumer to the Camel routing Error Handler,
* which mean any exceptions occurred while the consumer is trying to
* pickup incoming messages, or the likes, will now be processed as a
* message and handled by the routing Error Handler. By default the
* consumer will use the org.apache.camel.spi.ExceptionHandler to deal
* with exceptions, that will be logged at WARN or ERROR level and
* ignored.
*
* The option is a: <code>boolean</code> type.
*
* Default: false
* Group: consumer
*
* @param bridgeErrorHandler the value to set
* @return the dsl builder
*/
default MinaComponentBuilder bridgeErrorHandler(
boolean bridgeErrorHandler) {
doSetProperty("bridgeErrorHandler", bridgeErrorHandler);
return this;
}
/**
* If the clientMode is true, mina consumer will connect the address as
* a TCP client.
*
* The option is a: <code>boolean</code> type.
*
* Default: false
* Group: consumer
*
* @param clientMode the value to set
* @return the dsl builder
*/
default MinaComponentBuilder clientMode(boolean clientMode) {
doSetProperty("clientMode", clientMode);
return this;
}
/**
* If sync is enabled then this option dictates MinaConsumer if it
* should disconnect where there is no reply to send back.
*
* The option is a: <code>boolean</code> type.
*
* Default: true
* Group: consumer (advanced)
*
* @param disconnectOnNoReply the value to set
* @return the dsl builder
*/
default MinaComponentBuilder disconnectOnNoReply(
boolean disconnectOnNoReply) {
doSetProperty("disconnectOnNoReply", disconnectOnNoReply);
return this;
}
/**
* If sync is enabled this option dictates MinaConsumer which logging
* level to use when logging a there is no reply to send back.
*
* The option is a:
* <code>org.apache.camel.LoggingLevel</code> type.
*
* Default: WARN
* Group: consumer (advanced)
*
* @param noReplyLogLevel the value to set
* @return the dsl builder
*/
default MinaComponentBuilder noReplyLogLevel(
org.apache.camel.LoggingLevel noReplyLogLevel) {
doSetProperty("noReplyLogLevel", noReplyLogLevel);
return this;
}
/**
* Whether the producer should be started lazy (on the first message).
* By starting lazy you can use this to allow CamelContext and routes to
* startup in situations where a producer may otherwise fail during
* starting and cause the route to fail being started. By deferring this
* startup to be lazy then the startup failure can be handled during
* routing messages via Camel's routing error handlers. Beware that when
* the first message is processed then creating and starting the
* producer may take a little time and prolong the total processing time
* of the processing.
*
* The option is a: <code>boolean</code> type.
*
* Default: false
* Group: producer
*
* @param lazyStartProducer the value to set
* @return the dsl builder
*/
default MinaComponentBuilder lazyStartProducer(boolean lazyStartProducer) {
doSetProperty("lazyStartProducer", lazyStartProducer);
return this;
}
/**
* Whether to create the InetAddress once and reuse. Setting this to
* false allows to pickup DNS changes in the network.
*
* The option is a: <code>boolean</code> type.
*
* Default: true
* Group: producer (advanced)
*
* @param cachedAddress the value to set
* @return the dsl builder
*/
default MinaComponentBuilder cachedAddress(boolean cachedAddress) {
doSetProperty("cachedAddress", cachedAddress);
return this;
}
/**
* Sessions can be lazily created to avoid exceptions, if the remote
* server is not up and running when the Camel producer is started.
*
* The option is a: <code>boolean</code> type.
*
* Default: true
* Group: producer (advanced)
*
* @param lazySessionCreation the value to set
* @return the dsl builder
*/
default MinaComponentBuilder lazySessionCreation(
boolean lazySessionCreation) {
doSetProperty("lazySessionCreation", lazySessionCreation);
return this;
}
/**
* Whether autowiring is enabled. This is used for automatic autowiring
* options (the option must be marked as autowired) by looking up in the
* registry to find if there is a single instance of matching type,
* which then gets configured on the component. This can be used for
* automatic configuring JDBC data sources, JMS connection factories,
* AWS Clients, etc.
*
* The option is a: <code>boolean</code> type.
*
* Default: true
* Group: advanced
*
* @param autowiredEnabled the value to set
* @return the dsl builder
*/
default MinaComponentBuilder autowiredEnabled(boolean autowiredEnabled) {
doSetProperty("autowiredEnabled", autowiredEnabled);
return this;
}
/**
* To use the shared mina configuration.
*
* The option is a:
* <code>org.apache.camel.component.mina.MinaConfiguration</code> type.
*
* Group: advanced
*
* @param configuration the value to set
* @return the dsl builder
*/
default MinaComponentBuilder configuration(
org.apache.camel.component.mina.MinaConfiguration configuration) {
doSetProperty("configuration", configuration);
return this;
}
/**
* Number of worker threads in the worker pool for TCP and UDP.
*
* The option is a: <code>int</code> type.
*
* Default: 16
* Group: advanced
*
* @param maximumPoolSize the value to set
* @return the dsl builder
*/
default MinaComponentBuilder maximumPoolSize(int maximumPoolSize) {
doSetProperty("maximumPoolSize", maximumPoolSize);
return this;
}
/**
* Whether to use ordered thread pool, to ensure events are processed
* orderly on the same channel.
*
* The option is a: <code>boolean</code> type.
*
* Default: true
* Group: advanced
*
* @param orderedThreadPoolExecutor the value to set
* @return the dsl builder
*/
default MinaComponentBuilder orderedThreadPoolExecutor(
boolean orderedThreadPoolExecutor) {
doSetProperty("orderedThreadPoolExecutor", orderedThreadPoolExecutor);
return this;
}
/**
* Only used for TCP. You can transfer the exchange over the wire
* instead of just the body. The following fields are transferred: In
* body, Out body, fault body, In headers, Out headers, fault headers,
* exchange properties, exchange exception. This requires that the
* objects are serializable. Camel will exclude any non-serializable
* objects and log it at WARN level.
*
* The option is a: <code>boolean</code> type.
*
* Default: false
* Group: advanced
*
* @param transferExchange the value to set
* @return the dsl builder
*/
default MinaComponentBuilder transferExchange(boolean transferExchange) {
doSetProperty("transferExchange", transferExchange);
return this;
}
/**
* The mina component installs a default codec if both, codec is null
* and textline is false. Setting allowDefaultCodec to false prevents
* the mina component from installing a default codec as the first
* element in the filter chain. This is useful in scenarios where
* another filter must be the first in the filter chain, like the SSL
* filter.
*
* The option is a: <code>boolean</code> type.
*
* Default: true
* Group: codec
*
* @param allowDefaultCodec the value to set
* @return the dsl builder
*/
default MinaComponentBuilder allowDefaultCodec(boolean allowDefaultCodec) {
doSetProperty("allowDefaultCodec", allowDefaultCodec);
return this;
}
/**
* To use a custom minda codec implementation.
*
* The option is a:
* <code>org.apache.mina.filter.codec.ProtocolCodecFactory</code> type.
*
* Group: codec
*
* @param codec the value to set
* @return the dsl builder
*/
default MinaComponentBuilder codec(
org.apache.mina.filter.codec.ProtocolCodecFactory codec) {
doSetProperty("codec", codec);
return this;
}
/**
* To set the textline protocol decoder max line length. By default the
* default value of Mina itself is used which are 1024.
*
* The option is a: <code>int</code> type.
*
* Default: 1024
* Group: codec
*
* @param decoderMaxLineLength the value to set
* @return the dsl builder
*/
default MinaComponentBuilder decoderMaxLineLength(
int decoderMaxLineLength) {
doSetProperty("decoderMaxLineLength", decoderMaxLineLength);
return this;
}
/**
* To set the textline protocol encoder max line length. By default the
* default value of Mina itself is used which are Integer.MAX_VALUE.
*
* The option is a: <code>int</code> type.
*
* Default: -1
* Group: codec
*
* @param encoderMaxLineLength the value to set
* @return the dsl builder
*/
default MinaComponentBuilder encoderMaxLineLength(
int encoderMaxLineLength) {
doSetProperty("encoderMaxLineLength", encoderMaxLineLength);
return this;
}
/**
* You can configure the encoding (a charset name) to use for the TCP
* textline codec and the UDP protocol. If not provided, Camel will use
* the JVM default Charset.
*
* The option is a: <code>java.lang.String</code> type.
*
* Group: codec
*
* @param encoding the value to set
* @return the dsl builder
*/
default MinaComponentBuilder encoding(java.lang.String encoding) {
doSetProperty("encoding", encoding);
return this;
}
/**
* You can set a list of Mina IoFilters to use.
*
* The option is a:
* <code>java.util.List&lt;org.apache.mina.core.filterchain.IoFilter&gt;</code> type.
*
* Group: codec
*
* @param filters the value to set
* @return the dsl builder
*/
default MinaComponentBuilder filters(
java.util.List<org.apache.mina.core.filterchain.IoFilter> filters) {
doSetProperty("filters", filters);
return this;
}
/**
* Only used for TCP. If no codec is specified, you can use this flag to
* indicate a text line based codec; if not specified or the value is
* false, then Object Serialization is assumed over TCP.
*
* The option is a: <code>boolean</code> type.
*
* Default: false
* Group: codec
*
* @param textline the value to set
* @return the dsl builder
*/
default MinaComponentBuilder textline(boolean textline) {
doSetProperty("textline", textline);
return this;
}
/**
* Only used for TCP and if textline=true. Sets the text line delimiter
* to use. If none provided, Camel will use DEFAULT. This delimiter is
* used to mark the end of text.
*
* The option is a:
* <code>org.apache.camel.component.mina.MinaTextLineDelimiter</code> type.
*
* Group: codec
*
* @param textlineDelimiter the value to set
* @return the dsl builder
*/
default MinaComponentBuilder textlineDelimiter(
org.apache.camel.component.mina.MinaTextLineDelimiter textlineDelimiter) {
doSetProperty("textlineDelimiter", textlineDelimiter);
return this;
}
/**
* Whether to auto start SSL handshake.
*
* The option is a: <code>boolean</code> type.
*
* Default: true
* Group: security
*
* @param autoStartTls the value to set
* @return the dsl builder
*/
default MinaComponentBuilder autoStartTls(boolean autoStartTls) {
doSetProperty("autoStartTls", autoStartTls);
return this;
}
/**
* To configure SSL security.
*
* The option is a:
* <code>org.apache.camel.support.jsse.SSLContextParameters</code> type.
*
* Group: security
*
* @param sslContextParameters the value to set
* @return the dsl builder
*/
default MinaComponentBuilder sslContextParameters(
org.apache.camel.support.jsse.SSLContextParameters sslContextParameters) {
doSetProperty("sslContextParameters", sslContextParameters);
return this;
}
/**
* Enable usage of global SSL context parameters.
*
* The option is a: <code>boolean</code> type.
*
* Default: false
* Group: security
*
* @param useGlobalSslContextParameters the value to set
* @return the dsl builder
*/
default MinaComponentBuilder useGlobalSslContextParameters(
boolean useGlobalSslContextParameters) {
doSetProperty("useGlobalSslContextParameters", useGlobalSslContextParameters);
return this;
}
}
class MinaComponentBuilderImpl
extends
AbstractComponentBuilder<MinaComponent>
implements
MinaComponentBuilder {
@Override
protected MinaComponent buildConcreteComponent() {
return new MinaComponent();
}
private org.apache.camel.component.mina.MinaConfiguration getOrCreateConfiguration(
org.apache.camel.component.mina.MinaComponent component) {
if (component.getConfiguration() == null) {
component.setConfiguration(new org.apache.camel.component.mina.MinaConfiguration());
}
return component.getConfiguration();
}
@Override
protected boolean setPropertyOnComponent(
Component component,
String name,
Object value) {
switch (name) {
case "disconnect": getOrCreateConfiguration((MinaComponent) component).setDisconnect((boolean) value); return true;
case "minaLogger": getOrCreateConfiguration((MinaComponent) component).setMinaLogger((boolean) value); return true;
case "sync": getOrCreateConfiguration((MinaComponent) component).setSync((boolean) value); return true;
case "timeout": getOrCreateConfiguration((MinaComponent) component).setTimeout((long) value); return true;
case "writeTimeout": getOrCreateConfiguration((MinaComponent) component).setWriteTimeout((long) value); return true;
case "bridgeErrorHandler": ((MinaComponent) component).setBridgeErrorHandler((boolean) value); return true;
case "clientMode": getOrCreateConfiguration((MinaComponent) component).setClientMode((boolean) value); return true;
case "disconnectOnNoReply": getOrCreateConfiguration((MinaComponent) component).setDisconnectOnNoReply((boolean) value); return true;
case "noReplyLogLevel": getOrCreateConfiguration((MinaComponent) component).setNoReplyLogLevel((org.apache.camel.LoggingLevel) value); return true;
case "lazyStartProducer": ((MinaComponent) component).setLazyStartProducer((boolean) value); return true;
case "cachedAddress": getOrCreateConfiguration((MinaComponent) component).setCachedAddress((boolean) value); return true;
case "lazySessionCreation": getOrCreateConfiguration((MinaComponent) component).setLazySessionCreation((boolean) value); return true;
case "autowiredEnabled": ((MinaComponent) component).setAutowiredEnabled((boolean) value); return true;
case "configuration": ((MinaComponent) component).setConfiguration((org.apache.camel.component.mina.MinaConfiguration) value); return true;
case "maximumPoolSize": getOrCreateConfiguration((MinaComponent) component).setMaximumPoolSize((int) value); return true;
case "orderedThreadPoolExecutor": getOrCreateConfiguration((MinaComponent) component).setOrderedThreadPoolExecutor((boolean) value); return true;
case "transferExchange": getOrCreateConfiguration((MinaComponent) component).setTransferExchange((boolean) value); return true;
case "allowDefaultCodec": getOrCreateConfiguration((MinaComponent) component).setAllowDefaultCodec((boolean) value); return true;
case "codec": getOrCreateConfiguration((MinaComponent) component).setCodec((org.apache.mina.filter.codec.ProtocolCodecFactory) value); return true;
case "decoderMaxLineLength": getOrCreateConfiguration((MinaComponent) component).setDecoderMaxLineLength((int) value); return true;
case "encoderMaxLineLength": getOrCreateConfiguration((MinaComponent) component).setEncoderMaxLineLength((int) value); return true;
case "encoding": getOrCreateConfiguration((MinaComponent) component).setEncoding((java.lang.String) value); return true;
case "filters": getOrCreateConfiguration((MinaComponent) component).setFilters((java.util.List) value); return true;
case "textline": getOrCreateConfiguration((MinaComponent) component).setTextline((boolean) value); return true;
case "textlineDelimiter": getOrCreateConfiguration((MinaComponent) component).setTextlineDelimiter((org.apache.camel.component.mina.MinaTextLineDelimiter) value); return true;
case "autoStartTls": getOrCreateConfiguration((MinaComponent) component).setAutoStartTls((boolean) value); return true;
case "sslContextParameters": getOrCreateConfiguration((MinaComponent) component).setSslContextParameters((org.apache.camel.support.jsse.SSLContextParameters) value); return true;
case "useGlobalSslContextParameters": ((MinaComponent) component).setUseGlobalSslContextParameters((boolean) value); return true;
default: return false;
}
}
}
} | nikhilvibhav/camel | core/camel-componentdsl/src/generated/java/org/apache/camel/builder/component/dsl/MinaComponentBuilderFactory.java | Java | apache-2.0 | 24,837 |
/*
* Copyright 2016 SimplifyOps, Inc. (http://simplifyops.com)
*
* 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 com.dtolabs.rundeck.server.plugins.services;
import com.dtolabs.rundeck.core.plugins.BasePluggableProviderService;
import com.dtolabs.rundeck.core.plugins.ServiceProviderLoader;
import com.dtolabs.rundeck.plugins.ServiceNameConstants;
import com.dtolabs.rundeck.plugins.logging.StreamingLogReaderPlugin;
/** $INTERFACE is ... User: greg Date: 5/24/13 Time: 9:32 AM */
public class StreamingLogReaderPluginProviderService extends BasePluggableProviderService<StreamingLogReaderPlugin> {
public static final String SERVICE_NAME = ServiceNameConstants.StreamingLogReader;
private ServiceProviderLoader rundeckServerServiceProviderLoader;
public StreamingLogReaderPluginProviderService() {
super(SERVICE_NAME, StreamingLogReaderPlugin.class);
}
@Override
public ServiceProviderLoader getPluginManager() {
return getRundeckServerServiceProviderLoader();
}
public ServiceProviderLoader getRundeckServerServiceProviderLoader() {
return rundeckServerServiceProviderLoader;
}
public void setRundeckServerServiceProviderLoader(ServiceProviderLoader rundeckServerServiceProviderLoader) {
this.rundeckServerServiceProviderLoader = rundeckServerServiceProviderLoader;
}
@Override
public boolean isScriptPluggable() {
//for now
return false;
}
}
| damageboy/rundeck | rundeckapp/src/java/com/dtolabs/rundeck/server/plugins/services/StreamingLogReaderPluginProviderService.java | Java | apache-2.0 | 1,978 |
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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.apache.camel.example.cxf.jaxrs;
import org.apache.camel.example.cxf.jaxrs.resources.Book;
import org.apache.camel.example.cxf.jaxrs.resources.BookNotFoundFault;
import org.apache.camel.example.cxf.jaxrs.resources.BookStore;
public class Client {
void invoke() throws BookNotFoundFault {
// JAXWSClient invocation
JAXWSClient jaxwsClient = new JAXWSClient();
BookStore bookStore = jaxwsClient.getBookStore();
bookStore.addBook(new Book("Camel User Guide", 123L));
Book book = bookStore.getBook(123L);
System.out.println("Get the book with id 123. " + book);
try {
book = bookStore.getBook(124L);
System.out.println("Get the book with id 124. " + book);
} catch (Exception exception) {
System.out.println("Expected exception received: " + exception);
}
// JAXRSClient invocation
JAXRSClient jaxrsClient = new JAXRSClient();
bookStore = jaxrsClient.getBookStore();
bookStore.addBook(new Book("Karaf User Guide", 124L));
book = bookStore.getBook(124L);
System.out.println("Get the book with id 124. " + book);
try {
book = bookStore.getBook(126L);
System.out.println("Get the book with id 126. " + book);
} catch (Exception exception) {
System.out.println("Expected exception received: " + exception);
}
}
public static void main(String args[]) throws Exception {
Client client = new Client();
client.invoke();
}
}
| objectiser/camel | examples/camel-example-cxf/src/main/java/org/apache/camel/example/cxf/jaxrs/Client.java | Java | apache-2.0 | 2,440 |
/*
* Licensed to Elasticsearch under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch licenses this file to you 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.elasticsearch.search.aggregations.bucket.significant;
import org.apache.lucene.util.BytesRef;
import org.elasticsearch.ElasticsearchParseException;
import org.elasticsearch.Version;
import org.elasticsearch.common.io.stream.InputStreamStreamInput;
import org.elasticsearch.common.io.stream.OutputStreamStreamOutput;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentFactory;
import org.elasticsearch.common.xcontent.XContentParser;
import org.elasticsearch.common.xcontent.json.JsonXContent;
import org.elasticsearch.search.SearchShardTarget;
import org.elasticsearch.search.aggregations.InternalAggregations;
import org.elasticsearch.search.aggregations.bucket.significant.heuristics.ChiSquare;
import org.elasticsearch.search.aggregations.bucket.significant.heuristics.GND;
import org.elasticsearch.search.aggregations.bucket.significant.heuristics.JLHScore;
import org.elasticsearch.search.aggregations.bucket.significant.heuristics.MutualInformation;
import org.elasticsearch.search.aggregations.bucket.significant.heuristics.PercentageScore;
import org.elasticsearch.search.aggregations.bucket.significant.heuristics.SignificanceHeuristic;
import org.elasticsearch.search.aggregations.bucket.significant.heuristics.SignificanceHeuristicBuilder;
import org.elasticsearch.search.aggregations.bucket.significant.heuristics.SignificanceHeuristicParser;
import org.elasticsearch.search.aggregations.bucket.significant.heuristics.SignificanceHeuristicParserMapper;
import org.elasticsearch.search.internal.SearchContext;
import org.elasticsearch.test.ESTestCase;
import org.elasticsearch.test.TestSearchContext;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import static org.elasticsearch.test.VersionUtils.randomVersion;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.greaterThan;
import static org.hamcrest.Matchers.greaterThanOrEqualTo;
import static org.hamcrest.Matchers.lessThan;
import static org.hamcrest.Matchers.lessThanOrEqualTo;
/**
*
*/
public class SignificanceHeuristicTests extends ESTestCase {
static class SignificantTermsTestSearchContext extends TestSearchContext {
@Override
public int numberOfShards() {
return 1;
}
@Override
public SearchShardTarget shardTarget() {
return new SearchShardTarget("no node, this is a unit test", "no index, this is a unit test", 0);
}
}
// test that stream output can actually be read - does not replace bwc test
public void testStreamResponse() throws Exception {
Version version = randomVersion(random());
InternalSignificantTerms[] sigTerms = getRandomSignificantTerms(getRandomSignificanceheuristic());
// write
ByteArrayOutputStream outBuffer = new ByteArrayOutputStream();
OutputStreamStreamOutput out = new OutputStreamStreamOutput(outBuffer);
out.setVersion(version);
sigTerms[0].writeTo(out);
// read
ByteArrayInputStream inBuffer = new ByteArrayInputStream(outBuffer.toByteArray());
InputStreamStreamInput in = new InputStreamStreamInput(inBuffer);
in.setVersion(version);
sigTerms[1].readFrom(in);
assertTrue(sigTerms[1].significanceHeuristic.equals(sigTerms[0].significanceHeuristic));
}
InternalSignificantTerms[] getRandomSignificantTerms(SignificanceHeuristic heuristic) {
InternalSignificantTerms[] sTerms = new InternalSignificantTerms[2];
ArrayList<InternalSignificantTerms.Bucket> buckets = new ArrayList<>();
if (randomBoolean()) {
BytesRef term = new BytesRef("123.0");
buckets.add(new SignificantLongTerms.Bucket(1, 2, 3, 4, 123, InternalAggregations.EMPTY, null));
sTerms[0] = new SignificantLongTerms(10, 20, "some_name", null, 1, 1, heuristic, buckets,
Collections.EMPTY_LIST, null);
sTerms[1] = new SignificantLongTerms();
} else {
BytesRef term = new BytesRef("someterm");
buckets.add(new SignificantStringTerms.Bucket(term, 1, 2, 3, 4, InternalAggregations.EMPTY));
sTerms[0] = new SignificantStringTerms(10, 20, "some_name", 1, 1, heuristic, buckets, Collections.EMPTY_LIST,
null);
sTerms[1] = new SignificantStringTerms();
}
return sTerms;
}
SignificanceHeuristic getRandomSignificanceheuristic() {
List<SignificanceHeuristic> heuristics = new ArrayList<>();
heuristics.add(JLHScore.INSTANCE);
heuristics.add(new MutualInformation(randomBoolean(), randomBoolean()));
heuristics.add(new GND(randomBoolean()));
heuristics.add(new ChiSquare(randomBoolean(), randomBoolean()));
return heuristics.get(randomInt(3));
}
// test that
// 1. The output of the builders can actually be parsed
// 2. The parser does not swallow parameters after a significance heuristic was defined
public void testBuilderAndParser() throws Exception {
Set<SignificanceHeuristicParser> parsers = new HashSet<>();
SignificanceHeuristicParserMapper heuristicParserMapper = new SignificanceHeuristicParserMapper(parsers, null);
SearchContext searchContext = new SignificantTermsTestSearchContext();
// test jlh with string
assertTrue(parseFromString(heuristicParserMapper, searchContext, "\"jlh\":{}") instanceof JLHScore);
// test gnd with string
assertTrue(parseFromString(heuristicParserMapper, searchContext, "\"gnd\":{}") instanceof GND);
// test mutual information with string
boolean includeNegatives = randomBoolean();
boolean backgroundIsSuperset = randomBoolean();
assertThat(parseFromString(heuristicParserMapper, searchContext, "\"mutual_information\":{\"include_negatives\": " + includeNegatives + ", \"background_is_superset\":" + backgroundIsSuperset + "}"), equalTo((SignificanceHeuristic) (new MutualInformation(includeNegatives, backgroundIsSuperset))));
assertThat(parseFromString(heuristicParserMapper, searchContext, "\"chi_square\":{\"include_negatives\": " + includeNegatives + ", \"background_is_superset\":" + backgroundIsSuperset + "}"), equalTo((SignificanceHeuristic) (new ChiSquare(includeNegatives, backgroundIsSuperset))));
// test with builders
assertTrue(parseFromBuilder(heuristicParserMapper, searchContext, new JLHScore.JLHScoreBuilder()) instanceof JLHScore);
assertTrue(parseFromBuilder(heuristicParserMapper, searchContext, new GND.GNDBuilder(backgroundIsSuperset)) instanceof GND);
assertThat(parseFromBuilder(heuristicParserMapper, searchContext, new MutualInformation.MutualInformationBuilder(includeNegatives, backgroundIsSuperset)), equalTo((SignificanceHeuristic) new MutualInformation(includeNegatives, backgroundIsSuperset)));
assertThat(parseFromBuilder(heuristicParserMapper, searchContext, new ChiSquare.ChiSquareBuilder(includeNegatives, backgroundIsSuperset)), equalTo((SignificanceHeuristic) new ChiSquare(includeNegatives, backgroundIsSuperset)));
// test exceptions
String faultyHeuristicdefinition = "\"mutual_information\":{\"include_negatives\": false, \"some_unknown_field\": false}";
String expectedError = "unknown field [some_unknown_field]";
checkParseException(heuristicParserMapper, searchContext, faultyHeuristicdefinition, expectedError);
faultyHeuristicdefinition = "\"chi_square\":{\"unknown_field\": true}";
expectedError = "unknown field [unknown_field]";
checkParseException(heuristicParserMapper, searchContext, faultyHeuristicdefinition, expectedError);
faultyHeuristicdefinition = "\"jlh\":{\"unknown_field\": true}";
expectedError = "expected an empty object, but found ";
checkParseException(heuristicParserMapper, searchContext, faultyHeuristicdefinition, expectedError);
faultyHeuristicdefinition = "\"gnd\":{\"unknown_field\": true}";
expectedError = "unknown field [unknown_field]";
checkParseException(heuristicParserMapper, searchContext, faultyHeuristicdefinition, expectedError);
}
protected void checkParseException(SignificanceHeuristicParserMapper heuristicParserMapper, SearchContext searchContext, String faultyHeuristicDefinition, String expectedError) throws IOException {
try {
XContentParser stParser = JsonXContent.jsonXContent.createParser("{\"field\":\"text\", " + faultyHeuristicDefinition + ",\"min_doc_count\":200}");
stParser.nextToken();
new SignificantTermsParser(heuristicParserMapper).parse("testagg", stParser, searchContext);
fail();
} catch (ElasticsearchParseException e) {
assertTrue(e.getMessage().contains(expectedError));
}
}
protected SignificanceHeuristic parseFromBuilder(SignificanceHeuristicParserMapper heuristicParserMapper, SearchContext searchContext, SignificanceHeuristicBuilder significanceHeuristicBuilder) throws IOException {
SignificantTermsBuilder stBuilder = new SignificantTermsBuilder("testagg");
stBuilder.significanceHeuristic(significanceHeuristicBuilder).field("text").minDocCount(200);
XContentBuilder stXContentBuilder = XContentFactory.jsonBuilder();
stBuilder.internalXContent(stXContentBuilder, null);
XContentParser stParser = JsonXContent.jsonXContent.createParser(stXContentBuilder.string());
return parseSignificanceHeuristic(heuristicParserMapper, searchContext, stParser);
}
private SignificanceHeuristic parseSignificanceHeuristic(SignificanceHeuristicParserMapper heuristicParserMapper, SearchContext searchContext, XContentParser stParser) throws IOException {
stParser.nextToken();
SignificantTermsAggregatorFactory aggregatorFactory = (SignificantTermsAggregatorFactory) new SignificantTermsParser(heuristicParserMapper).parse("testagg", stParser, searchContext);
stParser.nextToken();
assertThat(aggregatorFactory.getBucketCountThresholds().getMinDocCount(), equalTo(200l));
assertThat(stParser.currentToken(), equalTo(null));
stParser.close();
return aggregatorFactory.getSignificanceHeuristic();
}
protected SignificanceHeuristic parseFromString(SignificanceHeuristicParserMapper heuristicParserMapper, SearchContext searchContext, String heuristicString) throws IOException {
XContentParser stParser = JsonXContent.jsonXContent.createParser("{\"field\":\"text\", " + heuristicString + ", \"min_doc_count\":200}");
return parseSignificanceHeuristic(heuristicParserMapper, searchContext, stParser);
}
void testBackgroundAssertions(SignificanceHeuristic heuristicIsSuperset, SignificanceHeuristic heuristicNotSuperset) {
try {
heuristicIsSuperset.getScore(2, 3, 1, 4);
fail();
} catch (IllegalArgumentException illegalArgumentException) {
assertNotNull(illegalArgumentException.getMessage());
assertTrue(illegalArgumentException.getMessage().contains("subsetFreq > supersetFreq"));
}
try {
heuristicIsSuperset.getScore(1, 4, 2, 3);
fail();
} catch (IllegalArgumentException illegalArgumentException) {
assertNotNull(illegalArgumentException.getMessage());
assertTrue(illegalArgumentException.getMessage().contains("subsetSize > supersetSize"));
}
try {
heuristicIsSuperset.getScore(2, 1, 3, 4);
fail();
} catch (IllegalArgumentException illegalArgumentException) {
assertNotNull(illegalArgumentException.getMessage());
assertTrue(illegalArgumentException.getMessage().contains("subsetFreq > subsetSize"));
}
try {
heuristicIsSuperset.getScore(1, 2, 4, 3);
fail();
} catch (IllegalArgumentException illegalArgumentException) {
assertNotNull(illegalArgumentException.getMessage());
assertTrue(illegalArgumentException.getMessage().contains("supersetFreq > supersetSize"));
}
try {
heuristicIsSuperset.getScore(1, 3, 4, 4);
fail();
} catch (IllegalArgumentException assertionError) {
assertNotNull(assertionError.getMessage());
assertTrue(assertionError.getMessage().contains("supersetFreq - subsetFreq > supersetSize - subsetSize"));
}
try {
int idx = randomInt(3);
long[] values = {1, 2, 3, 4};
values[idx] *= -1;
heuristicIsSuperset.getScore(values[0], values[1], values[2], values[3]);
fail();
} catch (IllegalArgumentException illegalArgumentException) {
assertNotNull(illegalArgumentException.getMessage());
assertTrue(illegalArgumentException.getMessage().contains("Frequencies of subset and superset must be positive"));
}
try {
heuristicNotSuperset.getScore(2, 1, 3, 4);
fail();
} catch (IllegalArgumentException illegalArgumentException) {
assertNotNull(illegalArgumentException.getMessage());
assertTrue(illegalArgumentException.getMessage().contains("subsetFreq > subsetSize"));
}
try {
heuristicNotSuperset.getScore(1, 2, 4, 3);
fail();
} catch (IllegalArgumentException illegalArgumentException) {
assertNotNull(illegalArgumentException.getMessage());
assertTrue(illegalArgumentException.getMessage().contains("supersetFreq > supersetSize"));
}
try {
int idx = randomInt(3);
long[] values = {1, 2, 3, 4};
values[idx] *= -1;
heuristicNotSuperset.getScore(values[0], values[1], values[2], values[3]);
fail();
} catch (IllegalArgumentException illegalArgumentException) {
assertNotNull(illegalArgumentException.getMessage());
assertTrue(illegalArgumentException.getMessage().contains("Frequencies of subset and superset must be positive"));
}
}
void testAssertions(SignificanceHeuristic heuristic) {
try {
int idx = randomInt(3);
long[] values = {1, 2, 3, 4};
values[idx] *= -1;
heuristic.getScore(values[0], values[1], values[2], values[3]);
fail();
} catch (IllegalArgumentException illegalArgumentException) {
assertNotNull(illegalArgumentException.getMessage());
assertTrue(illegalArgumentException.getMessage().contains("Frequencies of subset and superset must be positive"));
}
try {
heuristic.getScore(1, 2, 4, 3);
fail();
} catch (IllegalArgumentException illegalArgumentException) {
assertNotNull(illegalArgumentException.getMessage());
assertTrue(illegalArgumentException.getMessage().contains("supersetFreq > supersetSize"));
}
try {
heuristic.getScore(2, 1, 3, 4);
fail();
} catch (IllegalArgumentException illegalArgumentException) {
assertNotNull(illegalArgumentException.getMessage());
assertTrue(illegalArgumentException.getMessage().contains("subsetFreq > subsetSize"));
}
}
public void testAssertions() throws Exception {
testBackgroundAssertions(new MutualInformation(true, true), new MutualInformation(true, false));
testBackgroundAssertions(new ChiSquare(true, true), new ChiSquare(true, false));
testBackgroundAssertions(new GND(true), new GND(false));
testAssertions(PercentageScore.INSTANCE);
testAssertions(JLHScore.INSTANCE);
}
public void testBasicScoreProperties() {
basicScoreProperties(JLHScore.INSTANCE, true);
basicScoreProperties(new GND(true), true);
basicScoreProperties(PercentageScore.INSTANCE, true);
basicScoreProperties(new MutualInformation(true, true), false);
basicScoreProperties(new ChiSquare(true, true), false);
}
public void basicScoreProperties(SignificanceHeuristic heuristic, boolean test0) {
assertThat(heuristic.getScore(1, 1, 1, 3), greaterThan(0.0));
assertThat(heuristic.getScore(1, 1, 2, 3), lessThan(heuristic.getScore(1, 1, 1, 3)));
assertThat(heuristic.getScore(1, 1, 3, 4), lessThan(heuristic.getScore(1, 1, 2, 4)));
if (test0) {
assertThat(heuristic.getScore(0, 1, 2, 3), equalTo(0.0));
}
double score = 0.0;
try {
long a = randomLong();
long b = randomLong();
long c = randomLong();
long d = randomLong();
score = heuristic.getScore(a, b, c, d);
} catch (IllegalArgumentException e) {
}
assertThat(score, greaterThanOrEqualTo(0.0));
}
public void testScoreMutual() throws Exception {
SignificanceHeuristic heuristic = new MutualInformation(true, true);
assertThat(heuristic.getScore(1, 1, 1, 3), greaterThan(0.0));
assertThat(heuristic.getScore(1, 1, 2, 3), lessThan(heuristic.getScore(1, 1, 1, 3)));
assertThat(heuristic.getScore(2, 2, 2, 4), equalTo(1.0));
assertThat(heuristic.getScore(0, 2, 2, 4), equalTo(1.0));
assertThat(heuristic.getScore(2, 2, 4, 4), equalTo(0.0));
assertThat(heuristic.getScore(1, 2, 2, 4), equalTo(0.0));
assertThat(heuristic.getScore(3, 6, 9, 18), equalTo(0.0));
double score = 0.0;
try {
long a = randomLong();
long b = randomLong();
long c = randomLong();
long d = randomLong();
score = heuristic.getScore(a, b, c, d);
} catch (IllegalArgumentException e) {
}
assertThat(score, lessThanOrEqualTo(1.0));
assertThat(score, greaterThanOrEqualTo(0.0));
heuristic = new MutualInformation(false, true);
assertThat(heuristic.getScore(0, 1, 2, 3), equalTo(Double.NEGATIVE_INFINITY));
heuristic = new MutualInformation(true, false);
score = heuristic.getScore(2, 3, 1, 4);
assertThat(score, greaterThanOrEqualTo(0.0));
assertThat(score, lessThanOrEqualTo(1.0));
score = heuristic.getScore(1, 4, 2, 3);
assertThat(score, greaterThanOrEqualTo(0.0));
assertThat(score, lessThanOrEqualTo(1.0));
score = heuristic.getScore(1, 3, 4, 4);
assertThat(score, greaterThanOrEqualTo(0.0));
assertThat(score, lessThanOrEqualTo(1.0));
}
public void testGNDCornerCases() throws Exception {
GND gnd = new GND(true);
//term is only in the subset, not at all in the other set but that is because the other set is empty.
// this should actually not happen because only terms that are in the subset are considered now,
// however, in this case the score should be 0 because a term that does not exist cannot be relevant...
assertThat(gnd.getScore(0, randomIntBetween(1, 2), 0, randomIntBetween(2,3)), equalTo(0.0));
// the terms do not co-occur at all - should be 0
assertThat(gnd.getScore(0, randomIntBetween(1, 2), randomIntBetween(2, 3), randomIntBetween(5,6)), equalTo(0.0));
// comparison between two terms that do not exist - probably not relevant
assertThat(gnd.getScore(0, 0, 0, randomIntBetween(1,2)), equalTo(0.0));
// terms co-occur perfectly - should be 1
assertThat(gnd.getScore(1, 1, 1, 1), equalTo(1.0));
gnd = new GND(false);
assertThat(gnd.getScore(0, 0, 0, 0), equalTo(0.0));
}
}
| PhaedrusTheGreek/elasticsearch | core/src/test/java/org/elasticsearch/search/aggregations/bucket/significant/SignificanceHeuristicTests.java | Java | apache-2.0 | 20,764 |
/*
* Copyright 2000-2012 JetBrains s.r.o.
*
* 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 com.intellij.psi.impl;
import com.intellij.openapi.project.Project;
import com.intellij.psi.PsiClass;
import com.intellij.psi.PsiField;
import com.intellij.psi.PsiFile;
import com.intellij.psi.PsiMethod;
import com.intellij.psi.search.GlobalSearchScope;
import com.intellij.psi.search.PsiShortNamesCache;
import com.intellij.util.ArrayUtil;
import com.intellij.util.CommonProcessors;
import com.intellij.util.Processor;
import com.intellij.util.containers.ContainerUtil;
import com.intellij.util.indexing.IdFilter;
import gnu.trove.THashSet;
import org.jetbrains.annotations.NonNls;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.Arrays;
import java.util.Set;
public class CompositeShortNamesCache extends PsiShortNamesCache {
private final PsiShortNamesCache[] myCaches;
public CompositeShortNamesCache(Project project) {
myCaches = project.isDefault() ? new PsiShortNamesCache[0] : project.getExtensions(PsiShortNamesCache.EP_NAME);
}
@Override
@NotNull
public PsiFile[] getFilesByName(@NotNull String name) {
Merger<PsiFile> merger = null;
for (PsiShortNamesCache cache : myCaches) {
PsiFile[] classes = cache.getFilesByName(name);
if (classes.length != 0) {
if (merger == null) merger = new Merger<>();
merger.add(classes);
}
}
PsiFile[] result = merger == null ? null : merger.getResult();
return result != null ? result : PsiFile.EMPTY_ARRAY;
}
@Override
@NotNull
public String[] getAllFileNames() {
Merger<String> merger = new Merger<>();
for (PsiShortNamesCache cache : myCaches) {
merger.add(cache.getAllFileNames());
}
String[] result = merger.getResult();
return result != null ? result : ArrayUtil.EMPTY_STRING_ARRAY;
}
@Override
@NotNull
public PsiClass[] getClassesByName(@NotNull String name, @NotNull GlobalSearchScope scope) {
Merger<PsiClass> merger = null;
for (PsiShortNamesCache cache : myCaches) {
PsiClass[] classes = cache.getClassesByName(name, scope);
if (classes.length != 0) {
if (merger == null) merger = new Merger<>();
merger.add(classes);
}
}
PsiClass[] result = merger == null ? null : merger.getResult();
return result != null ? result : PsiClass.EMPTY_ARRAY;
}
@Override
@NotNull
public String[] getAllClassNames() {
Merger<String> merger = new Merger<>();
for (PsiShortNamesCache cache : myCaches) {
String[] names = cache.getAllClassNames();
merger.add(names);
}
String[] result = merger.getResult();
return result != null ? result : ArrayUtil.EMPTY_STRING_ARRAY;
}
@Override
public boolean processAllClassNames(@NotNull Processor<String> processor) {
CommonProcessors.UniqueProcessor<String> uniqueProcessor = new CommonProcessors.UniqueProcessor<>(processor);
for (PsiShortNamesCache cache : myCaches) {
if (!cache.processAllClassNames(uniqueProcessor)) {
return false;
}
}
return true;
}
@Override
public boolean processAllClassNames(@NotNull Processor<String> processor, @NotNull GlobalSearchScope scope, IdFilter filter) {
for (PsiShortNamesCache cache : myCaches) {
if (!cache.processAllClassNames(processor, scope, filter)) {
return false;
}
}
return true;
}
@Override
public boolean processAllMethodNames(@NotNull Processor<String> processor, @NotNull GlobalSearchScope scope, IdFilter filter) {
for (PsiShortNamesCache cache : myCaches) {
if (!cache.processAllMethodNames(processor, scope, filter)) {
return false;
}
}
return true;
}
@Override
public boolean processAllFieldNames(@NotNull Processor<String> processor, @NotNull GlobalSearchScope scope, IdFilter filter) {
for (PsiShortNamesCache cache : myCaches) {
if (!cache.processAllFieldNames(processor, scope, filter)) {
return false;
}
}
return true;
}
@Override
@NotNull
public PsiMethod[] getMethodsByName(@NotNull String name, @NotNull GlobalSearchScope scope) {
Merger<PsiMethod> merger = null;
for (PsiShortNamesCache cache : myCaches) {
PsiMethod[] methods = cache.getMethodsByName(name, scope);
if (methods.length != 0) {
if (merger == null) merger = new Merger<>();
merger.add(methods);
}
}
PsiMethod[] result = merger == null ? null : merger.getResult();
return result == null ? PsiMethod.EMPTY_ARRAY : result;
}
@Override
@NotNull
public PsiMethod[] getMethodsByNameIfNotMoreThan(@NonNls @NotNull final String name, @NotNull final GlobalSearchScope scope, final int maxCount) {
Merger<PsiMethod> merger = null;
for (PsiShortNamesCache cache : myCaches) {
PsiMethod[] methods = cache.getMethodsByNameIfNotMoreThan(name, scope, maxCount);
if (methods.length == maxCount) return methods;
if (methods.length != 0) {
if (merger == null) merger = new Merger<>();
merger.add(methods);
}
}
PsiMethod[] result = merger == null ? null : merger.getResult();
return result == null ? PsiMethod.EMPTY_ARRAY : result;
}
@NotNull
@Override
public PsiField[] getFieldsByNameIfNotMoreThan(@NonNls @NotNull String name, @NotNull GlobalSearchScope scope, int maxCount) {
Merger<PsiField> merger = null;
for (PsiShortNamesCache cache : myCaches) {
PsiField[] fields = cache.getFieldsByNameIfNotMoreThan(name, scope, maxCount);
if (fields.length == maxCount) return fields;
if (fields.length != 0) {
if (merger == null) merger = new Merger<>();
merger.add(fields);
}
}
PsiField[] result = merger == null ? null : merger.getResult();
return result == null ? PsiField.EMPTY_ARRAY : result;
}
@Override
public boolean processMethodsWithName(@NonNls @NotNull String name,
@NotNull GlobalSearchScope scope,
@NotNull Processor<PsiMethod> processor) {
return processMethodsWithName(name, processor, scope, null);
}
@Override
public boolean processMethodsWithName(@NonNls @NotNull String name,
@NotNull Processor<? super PsiMethod> processor,
@NotNull GlobalSearchScope scope,
@Nullable IdFilter idFilter) {
for (PsiShortNamesCache cache : myCaches) {
if (!cache.processMethodsWithName(name, processor, scope, idFilter)) return false;
}
return true;
}
@Override
@NotNull
public String[] getAllMethodNames() {
Merger<String> merger = new Merger<>();
for (PsiShortNamesCache cache : myCaches) {
merger.add(cache.getAllMethodNames());
}
String[] result = merger.getResult();
return result != null ? result : ArrayUtil.EMPTY_STRING_ARRAY;
}
@Override
@NotNull
public PsiField[] getFieldsByName(@NotNull String name, @NotNull GlobalSearchScope scope) {
Merger<PsiField> merger = null;
for (PsiShortNamesCache cache : myCaches) {
PsiField[] classes = cache.getFieldsByName(name, scope);
if (classes.length != 0) {
if (merger == null) merger = new Merger<>();
merger.add(classes);
}
}
PsiField[] result = merger == null ? null : merger.getResult();
return result == null ? PsiField.EMPTY_ARRAY : result;
}
@Override
@NotNull
public String[] getAllFieldNames() {
Merger<String> merger = null;
for (PsiShortNamesCache cache : myCaches) {
String[] classes = cache.getAllFieldNames();
if (classes.length != 0) {
if (merger == null) merger = new Merger<>();
merger.add(classes);
}
}
String[] result = merger == null ? null : merger.getResult();
return result == null ? ArrayUtil.EMPTY_STRING_ARRAY : result;
}
@Override
public boolean processFieldsWithName(@NotNull String key,
@NotNull Processor<? super PsiField> processor,
@NotNull GlobalSearchScope scope,
@Nullable IdFilter filter) {
for (PsiShortNamesCache cache : myCaches) {
if (!cache.processFieldsWithName(key, processor, scope, filter)) return false;
}
return true;
}
@Override
public boolean processClassesWithName(@NotNull String key,
@NotNull Processor<? super PsiClass> processor,
@NotNull GlobalSearchScope scope,
@Nullable IdFilter filter) {
for (PsiShortNamesCache cache : myCaches) {
if (!cache.processClassesWithName(key, processor, scope, filter)) return false;
}
return true;
}
private static class Merger<T> {
private T[] mySingleItem;
private Set<T> myAllItems;
public void add(@NotNull T[] items) {
if (items.length == 0) return;
if (mySingleItem == null) {
mySingleItem = items;
return;
}
if (myAllItems == null) {
T[] elements = mySingleItem;
myAllItems = ContainerUtil.addAll(new THashSet<>(elements.length), elements);
}
ContainerUtil.addAll(myAllItems, items);
}
public T[] getResult() {
if (myAllItems == null) return mySingleItem;
return myAllItems.toArray(mySingleItem);
}
}
@SuppressWarnings({"HardCodedStringLiteral"})
@Override
public String toString() {
return "Composite cache: " + Arrays.asList(myCaches);
}
}
| jk1/intellij-community | java/java-indexing-impl/src/com/intellij/psi/impl/CompositeShortNamesCache.java | Java | apache-2.0 | 10,218 |
/*************************GO-LICENSE-START*********************************
* Copyright 2014 ThoughtWorks, Inc.
*
* 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.
*************************GO-LICENSE-END***********************************/
package com.thoughtworks.go.domain.materials.git;
import com.thoughtworks.go.domain.materials.Modification;
import com.thoughtworks.go.util.DateUtils;
import java.util.LinkedList;
import java.util.List;
import java.util.Optional;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class GitModificationParser {
private LinkedList<Modification> modifications = new LinkedList<>();
private static final String SPACES = "\\s+";
private static final String COMMENT_INDENT = "\\s{4}";
private static final String COMMENT_TEXT = "(.*)";
private static final String HASH = "(\\w+)";
private static final String DATE = "(.+)";
private static final String AUTHOR = "(.+)";
private static final Pattern COMMIT_PATTERN = Pattern.compile("^commit" + SPACES + HASH + "$");
private static final Pattern AUTHOR_PATTERN = Pattern.compile("^Author:"+ SPACES + AUTHOR + "$");
private static final Pattern DATE_PATTERN = Pattern.compile("^Date:" + SPACES + DATE + "$");
private static final Pattern COMMENT_PATTERN = Pattern.compile("^" + COMMENT_INDENT + COMMENT_TEXT + "$");
public List<Modification> parse(List<String> output) {
for (String line : output) {
processLine(line);
}
return modifications;
}
public List<Modification> getModifications() {
return modifications;
}
public void processLine(String line) {
Matcher matcher = COMMIT_PATTERN.matcher(line);
if (matcher.matches()) {
modifications.add(new Modification("", "", null, null, matcher.group(1)));
}
Matcher authorMatcher = AUTHOR_PATTERN.matcher(line);
if (authorMatcher.matches()) {
modifications.getLast().setUserName(authorMatcher.group(1));
}
Matcher dateMatcher = DATE_PATTERN.matcher(line);
if (dateMatcher.matches()) {
modifications.getLast().setModifiedTime(DateUtils.parseISO8601(dateMatcher.group(1)));
}
Matcher commentMatcher = COMMENT_PATTERN.matcher(line);
if (commentMatcher.matches()) {
Modification last = modifications.getLast();
String comment = Optional.ofNullable(last.getComment()).orElse("");
if (!comment.isEmpty()) comment += "\n";
last.setComment(comment + commentMatcher.group(1));
}
}
}
| varshavaradarajan/gocd | domain/src/main/java/com/thoughtworks/go/domain/materials/git/GitModificationParser.java | Java | apache-2.0 | 3,128 |
/*
* $Id$
*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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.apache.struts2.showcase.chat;
import java.io.Serializable;
import java.util.Date;
/**
* Represends a user in the Chat example.
*/
public class User implements Serializable {
private static final long serialVersionUID = -1434958919516089297L;
private String name;
private Date creationDate;
public User(String name) {
this.name = name;
this.creationDate = new Date(System.currentTimeMillis());
}
public Date getCreationDate() {
return creationDate;
}
public String getName() {
return name;
}
}
| Ile2/struts2-showcase-demo | src/apps/showcase/src/main/java/org/apache/struts2/showcase/chat/User.java | Java | apache-2.0 | 1,350 |
// Copyright 2000-2021 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
package org.jetbrains.idea.svn;
import com.intellij.openapi.vcs.VcsConfiguration;
import com.intellij.openapi.vcs.changes.Change;
import com.intellij.openapi.vfs.LocalFileSystem;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.vcsUtil.VcsUtil;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.idea.svn.conflict.ConflictAction;
import org.jetbrains.idea.svn.conflict.ConflictOperation;
import org.jetbrains.idea.svn.conflict.ConflictVersion;
import org.jetbrains.idea.svn.conflict.TreeConflictDescription;
import org.junit.Before;
import org.junit.Test;
import java.io.File;
import java.util.function.BiConsumer;
import static com.intellij.testFramework.EdtTestUtil.runInEdtAndWait;
import static org.junit.Assert.*;
public class SvnTreeConflictDataTest extends SvnTestCase {
private VirtualFile myTheirs;
private SvnClientRunnerImpl mySvnClientRunner;
@Override
@Before
public void before() throws Exception {
myWcRootName = "wcRootConflictData";
myTraceClient = true;
super.before();
disableSilentOperation(VcsConfiguration.StandardConfirmation.ADD);
myTheirs = myTempDirFixture.findOrCreateDir("theirs");
mySvnClientRunner = new SvnClientRunnerImpl(myRunner);
mySvnClientRunner.checkout(myRepoUrl, myTheirs);
}
@Test
public void testFile2File_MINE_UNV_THEIRS_ADD() throws Exception {
assertConflict(TreeConflictData.FileToFile.MINE_UNV_THEIRS_ADD, false, (beforeDescription, afterDescription) -> {
assertEquals(ConflictOperation.UPDATE, beforeDescription.getOperation());
assertEquals(ConflictAction.ADD, beforeDescription.getConflictAction());
assertNull(beforeDescription.getSourceLeftVersion());
final ConflictVersion version = beforeDescription.getSourceRightVersion();
assertNotNull(version);
assertTrue(version.isFile());
});
}
@Test
public void testFile2File_MINE_EDIT_THEIRS_DELETE() throws Exception {
assertConflict(TreeConflictData.FileToFile.MINE_EDIT_THEIRS_DELETE, (beforeDescription, afterDescription) -> {
assertEquals(ConflictOperation.UPDATE, beforeDescription.getOperation());
assertEquals(ConflictAction.DELETE, beforeDescription.getConflictAction());
ConflictVersion leftVersion = beforeDescription.getSourceLeftVersion();
assertNotNull(leftVersion);
assertTrue(leftVersion.isFile());
final ConflictVersion version = beforeDescription.getSourceRightVersion();
assertNotNull(version);
assertTrue(version.isNone());
});
}
private String createConflict(@NotNull TreeConflictData.Data data, boolean createSubtree) throws Exception {
if (createSubtree) {
mySvnClientRunner.testSvnVersion(myWorkingCopyDir);
createSubTree();
}
runInEdtAndWait(() -> new ConflictCreator(vcs, myTheirs, myWorkingCopyDir, data, mySvnClientRunner).create());
return data.getConflictFile();
}
@Test
public void testFile2File_MINE_DELETE_THEIRS_EDIT() throws Exception {
assertConflict(TreeConflictData.FileToFile.MINE_DELETE_THEIRS_EDIT, (beforeDescription, afterDescription) -> {
assertEquals(ConflictOperation.UPDATE, beforeDescription.getOperation());
assertEquals(ConflictAction.EDIT, beforeDescription.getConflictAction());
ConflictVersion leftVersion = beforeDescription.getSourceLeftVersion();
assertNotNull(leftVersion);
assertTrue(leftVersion.isFile());
final ConflictVersion version = beforeDescription.getSourceRightVersion();
assertNotNull(version);
assertTrue(version.isFile());
});
}
@Test
public void testFile2File_MINE_EDIT_THEIRS_MOVE() throws Exception {
assertConflict(TreeConflictData.FileToFile.MINE_EDIT_THEIRS_MOVE, (beforeDescription, afterDescription) -> {
assertEquals(ConflictOperation.UPDATE, beforeDescription.getOperation());
assertEquals(ConflictAction.DELETE, beforeDescription.getConflictAction());
ConflictVersion leftVersion = beforeDescription.getSourceLeftVersion();
assertNotNull(leftVersion);
assertTrue(leftVersion.isFile());
final ConflictVersion version = beforeDescription.getSourceRightVersion();
assertNotNull(version);
assertTrue(version.isNone());
});
}
@Test
public void testFile2File_MINE_UNV_THEIRS_MOVE() throws Exception {
assertConflict(TreeConflictData.FileToFile.MINE_UNV_THEIRS_MOVE, (beforeDescription, afterDescription) -> {
assertEquals(ConflictOperation.UPDATE, beforeDescription.getOperation());
assertEquals(ConflictAction.ADD, beforeDescription.getConflictAction());
ConflictVersion leftVersion = beforeDescription.getSourceLeftVersion();
assertNull(leftVersion);
final ConflictVersion version = beforeDescription.getSourceRightVersion();
assertNotNull(version);
assertTrue(version.isFile());
});
}
@Test
public void testFile2File_MINE_MOVE_THEIRS_EDIT() throws Exception {
assertConflict(TreeConflictData.FileToFile.MINE_MOVE_THEIRS_EDIT, (beforeDescription, afterDescription) -> {
assertEquals(ConflictOperation.UPDATE, beforeDescription.getOperation());
assertEquals(ConflictAction.EDIT, beforeDescription.getConflictAction());
ConflictVersion leftVersion = beforeDescription.getSourceLeftVersion();
assertNotNull(leftVersion);
assertTrue(leftVersion.isFile());
final ConflictVersion version = beforeDescription.getSourceRightVersion();
assertNotNull(version);
assertTrue(version.isFile());
});
}
@Test
public void testFile2File_MINE_MOVE_THEIRS_ADD() throws Exception {
assertConflict(TreeConflictData.FileToFile.MINE_MOVE_THEIRS_ADD, (beforeDescription, afterDescription) -> {
assertEquals(ConflictOperation.UPDATE, beforeDescription.getOperation());
assertEquals(ConflictAction.ADD, beforeDescription.getConflictAction());
ConflictVersion leftVersion = beforeDescription.getSourceLeftVersion();
assertNull(leftVersion);
//Assert.assertEquals(NodeKind.FILE, leftVersion.getKind());
final ConflictVersion version = beforeDescription.getSourceRightVersion();
assertNotNull(version);
assertTrue(version.isFile());
});
}
//---------------------------------- dirs --------------------------------------------------------
@Test
public void testDir2Dir_MINE_UNV_THEIRS_ADD() throws Exception {
assertConflict(TreeConflictData.DirToDir.MINE_UNV_THEIRS_ADD, (beforeDescription, afterDescription) -> {
assertEquals(ConflictOperation.UPDATE, beforeDescription.getOperation());
assertEquals(ConflictAction.ADD, beforeDescription.getConflictAction());
ConflictVersion leftVersion = beforeDescription.getSourceLeftVersion();
assertNull(leftVersion);
final ConflictVersion version = beforeDescription.getSourceRightVersion();
assertNotNull(version);
assertTrue(version.isDirectory());
});
}
// not a conflict in Subversion 1.7.7. "mine" file becomes added
/*@Test
public void testDir2Dir_MINE_EDIT_THEIRS_DELETE() throws Exception {
final String conflictFile = createConflict(TreeConflictData.DirToDir.MINE_EDIT_THEIRS_DELETE);
VcsDirtyScopeManager.getInstance(myProject).markEverythingDirty();
ChangeListManager changeListManager = ChangeListManager.getInstance(myProject);
changeListManager.ensureUpToDate(false);
VirtualFile vf = LocalFileSystem.getInstance().refreshAndFindFileByIoFile(new File(myWorkingCopyDir.getPath(), conflictFile));
Assert.assertNotNull(vf);
final Change change = changeListManager.getChange(vf);
Assert.assertTrue(change instanceof ConflictedSvnChange);
TreeConflictDescription beforeDescription = ((ConflictedSvnChange)change).getBeforeDescription();
Assert.assertNotNull(beforeDescription);
final TreeConflictDescription afterDescription = ((ConflictedSvnChange)change).getAfterDescription();
Assert.assertNull(afterDescription);
Assert.assertEquals(ConflictOperation.UPDATE, beforeDescription.getOperation());
Assert.assertEquals(ConflictAction.DELETE, beforeDescription.getConflictAction());
ConflictVersion leftVersion = beforeDescription.getSourceLeftVersion();
Assert.assertNotNull(leftVersion);
Assert.assertEquals(NodeKind.DIR, leftVersion.getKind());
final ConflictVersion version = beforeDescription.getSourceRightVersion();
Assert.assertNotNull(version);
Assert.assertEquals(NodeKind.NONE, version.getKind());
}*/
@Test
public void testDir2Dir_MINE_DELETE_THEIRS_EDIT() throws Exception {
assertConflict(TreeConflictData.DirToDir.MINE_DELETE_THEIRS_EDIT, (beforeDescription, afterDescription) -> {
assertEquals(ConflictOperation.UPDATE, beforeDescription.getOperation());
assertEquals(ConflictAction.EDIT, beforeDescription.getConflictAction());
ConflictVersion leftVersion = beforeDescription.getSourceLeftVersion();
assertNotNull(leftVersion);
assertTrue(leftVersion.isDirectory());
final ConflictVersion version = beforeDescription.getSourceRightVersion();
assertNotNull(version);
assertTrue(version.isDirectory());
});
}
@Test
public void testDir2Dir_MINE_EDIT_THEIRS_MOVE() throws Exception {
assertConflict(TreeConflictData.DirToDir.MINE_EDIT_THEIRS_MOVE, (beforeDescription, afterDescription) -> {
assertEquals(ConflictOperation.UPDATE, beforeDescription.getOperation());
assertEquals(ConflictAction.DELETE, beforeDescription.getConflictAction());
ConflictVersion leftVersion = beforeDescription.getSourceLeftVersion();
assertNotNull(leftVersion);
assertTrue(leftVersion.isDirectory());
final ConflictVersion version = beforeDescription.getSourceRightVersion();
assertNotNull(version);
assertTrue(version.isNone());
});
}
@Test
public void testDir2Dir_MINE_UNV_THEIRS_MOVE() throws Exception {
assertConflict(TreeConflictData.DirToDir.MINE_UNV_THEIRS_MOVE, (beforeDescription, afterDescription) -> {
assertEquals(ConflictOperation.UPDATE, beforeDescription.getOperation());
assertEquals(ConflictAction.ADD, beforeDescription.getConflictAction());
ConflictVersion leftVersion = beforeDescription.getSourceLeftVersion();
assertNull(leftVersion);
final ConflictVersion version = beforeDescription.getSourceRightVersion();
assertNotNull(version);
assertTrue(version.isDirectory());
});
}
@Test
public void testDir2Dir_MINE_MOVE_THEIRS_EDIT() throws Exception {
assertConflict(TreeConflictData.DirToDir.MINE_MOVE_THEIRS_EDIT, (beforeDescription, afterDescription) -> {
assertEquals(ConflictOperation.UPDATE, beforeDescription.getOperation());
assertEquals(ConflictAction.EDIT, beforeDescription.getConflictAction());
ConflictVersion leftVersion = beforeDescription.getSourceLeftVersion();
assertNotNull(leftVersion);
assertTrue(leftVersion.isDirectory());
final ConflictVersion version = beforeDescription.getSourceRightVersion();
assertNotNull(version);
assertTrue(version.isDirectory());
});
}
@Test
public void testDir2Dir_MINE_MOVE_THEIRS_ADD() throws Exception {
assertConflict(TreeConflictData.DirToDir.MINE_MOVE_THEIRS_ADD, (beforeDescription, afterDescription) -> {
assertEquals(ConflictOperation.UPDATE, beforeDescription.getOperation());
assertEquals(ConflictAction.ADD, beforeDescription.getConflictAction());
ConflictVersion leftVersion = beforeDescription.getSourceLeftVersion();
assertNull(leftVersion);
//Assert.assertEquals(NodeKind.DIR, leftVersion.getKind());
final ConflictVersion version = beforeDescription.getSourceRightVersion();
assertNotNull(version);
assertTrue(version.isDirectory());
});
}
//---------------------------------
@Test
public void testFile2Dir_MINE_UNV_THEIRS_ADD() throws Exception {
assertConflict(TreeConflictData.FileToDir.MINE_UNV_THEIRS_ADD, (beforeDescription, afterDescription) -> {
assertEquals(ConflictOperation.UPDATE, beforeDescription.getOperation());
assertEquals(ConflictAction.ADD, beforeDescription.getConflictAction());
ConflictVersion leftVersion = beforeDescription.getSourceLeftVersion();
assertNull(leftVersion);
final ConflictVersion version = beforeDescription.getSourceRightVersion();
assertNotNull(version);
assertTrue(version.isDirectory());
});
}
@Test
public void testFile2Dir_MINE_ADD_THEIRS_ADD() throws Exception {
assertConflict(TreeConflictData.FileToDir.MINE_ADD_THEIRS_ADD, (beforeDescription, afterDescription) -> {
assertEquals(ConflictOperation.UPDATE, beforeDescription.getOperation());
assertEquals(ConflictAction.ADD, beforeDescription.getConflictAction());
ConflictVersion leftVersion = beforeDescription.getSourceLeftVersion();
assertNull(leftVersion);
final ConflictVersion version = beforeDescription.getSourceRightVersion();
assertNotNull(version);
assertTrue(version.isDirectory());
});
}
@Test
public void testFile2Dir_MINE_UNV_THEIRS_MOVE() throws Exception {
assertConflict(TreeConflictData.FileToDir.MINE_UNV_THEIRS_MOVE, (beforeDescription, afterDescription) -> {
assertEquals(ConflictOperation.UPDATE, beforeDescription.getOperation());
assertEquals(ConflictAction.ADD, beforeDescription.getConflictAction());
ConflictVersion leftVersion = beforeDescription.getSourceLeftVersion();
assertNull(leftVersion);
final ConflictVersion version = beforeDescription.getSourceRightVersion();
assertNotNull(version);
assertTrue(version.isDirectory());
});
}
@Test
public void testFile2Dir_MINE_ADD_THEIRS_MOVE() throws Exception {
assertConflict(TreeConflictData.FileToDir.MINE_ADD_THEIRS_MOVE, (beforeDescription, afterDescription) -> {
assertEquals(ConflictOperation.UPDATE, beforeDescription.getOperation());
assertEquals(ConflictAction.ADD, beforeDescription.getConflictAction());
ConflictVersion leftVersion = beforeDescription.getSourceLeftVersion();
assertNull(leftVersion);
final ConflictVersion version = beforeDescription.getSourceRightVersion();
assertNotNull(version);
assertTrue(version.isDirectory());
});
}
@Test
public void testFile2Dir_MINE_MOVE_THEIRS_ADD() throws Exception {
assertConflict(TreeConflictData.FileToDir.MINE_MOVE_THEIRS_ADD, (beforeDescription, afterDescription) -> {
assertEquals(ConflictOperation.UPDATE, beforeDescription.getOperation());
assertEquals(ConflictAction.ADD, beforeDescription.getConflictAction());
ConflictVersion leftVersion = beforeDescription.getSourceLeftVersion();
assertNull(leftVersion);
final ConflictVersion version = beforeDescription.getSourceRightVersion();
assertNotNull(version);
assertTrue(version.isDirectory());
});
}
//******************************************
// dir -> file (mine, theirs)
@Test
public void testDir2File_MINE_UNV_THEIRS_ADD() throws Exception {
assertConflict(TreeConflictData.DirToFile.MINE_UNV_THEIRS_ADD, (beforeDescription, afterDescription) -> {
assertEquals(ConflictOperation.UPDATE, beforeDescription.getOperation());
assertEquals(ConflictAction.ADD, beforeDescription.getConflictAction());
ConflictVersion leftVersion = beforeDescription.getSourceLeftVersion();
assertNull(leftVersion);
final ConflictVersion version = beforeDescription.getSourceRightVersion();
assertNotNull(version);
assertTrue(version.isFile());
});
}
@Test
public void testDir2File_MINE_ADD_THEIRS_ADD() throws Exception {
assertConflict(TreeConflictData.DirToFile.MINE_ADD_THEIRS_ADD, (beforeDescription, afterDescription) -> {
assertEquals(ConflictOperation.UPDATE, beforeDescription.getOperation());
assertEquals(ConflictAction.ADD, beforeDescription.getConflictAction());
ConflictVersion leftVersion = beforeDescription.getSourceLeftVersion();
assertNull(leftVersion);
final ConflictVersion version = beforeDescription.getSourceRightVersion();
assertNotNull(version);
assertTrue(version.isFile());
});
}
@Test
public void testDir2File_MINE_UNV_THEIRS_MOVE() throws Exception {
assertConflict(TreeConflictData.DirToFile.MINE_UNV_THEIRS_MOVE, (beforeDescription, afterDescription) -> {
assertEquals(ConflictOperation.UPDATE, beforeDescription.getOperation());
assertEquals(ConflictAction.ADD, beforeDescription.getConflictAction());
ConflictVersion leftVersion = beforeDescription.getSourceLeftVersion();
assertNull(leftVersion);
final ConflictVersion version = beforeDescription.getSourceRightVersion();
assertNotNull(version);
assertTrue(version.isFile());
});
}
@Test
public void testDir2File_MINE_ADD_THEIRS_MOVE() throws Exception {
assertConflict(TreeConflictData.DirToFile.MINE_ADD_THEIRS_MOVE, (beforeDescription, afterDescription) -> {
assertEquals(ConflictOperation.UPDATE, beforeDescription.getOperation());
assertEquals(ConflictAction.ADD, beforeDescription.getConflictAction());
ConflictVersion leftVersion = beforeDescription.getSourceLeftVersion();
assertNull(leftVersion);
final ConflictVersion version = beforeDescription.getSourceRightVersion();
assertNotNull(version);
assertTrue(version.isFile());
});
}
@Test
public void testDir2File_MINE_MOVE_THEIRS_ADD() throws Exception {
assertConflict(TreeConflictData.DirToFile.MINE_MOVE_THEIRS_ADD, (beforeDescription, afterDescription) -> {
assertEquals(ConflictOperation.UPDATE, beforeDescription.getOperation());
assertEquals(ConflictAction.ADD, beforeDescription.getConflictAction());
ConflictVersion leftVersion = beforeDescription.getSourceLeftVersion();
assertNull(leftVersion);
final ConflictVersion version = beforeDescription.getSourceRightVersion();
assertNotNull(version);
assertTrue(version.isFile());
});
}
private void createSubTree() throws Exception {
enableSilentOperation(VcsConfiguration.StandardConfirmation.ADD);
enableSilentOperation(VcsConfiguration.StandardConfirmation.REMOVE);
new SubTree(myWorkingCopyDir);
mySvnClientRunner.checkin(myWorkingCopyDir);
mySvnClientRunner.update(myTheirs);
mySvnClientRunner.update(myWorkingCopyDir);
disableSilentOperation(VcsConfiguration.StandardConfirmation.ADD);
disableSilentOperation(VcsConfiguration.StandardConfirmation.REMOVE);
}
private void assertConflict(@NotNull TreeConflictData.Data data,
@NotNull BiConsumer<TreeConflictDescription, TreeConflictDescription> checker) throws Exception {
assertConflict(data, true, checker);
}
private void assertConflict(@NotNull TreeConflictData.Data data,
boolean createSubtree,
@NotNull BiConsumer<TreeConflictDescription, TreeConflictDescription> checker) throws Exception {
String conflictFile = createConflict(data, createSubtree);
refreshChanges();
Change change;
if (data == TreeConflictData.DirToDir.MINE_DELETE_THEIRS_EDIT ||
data == TreeConflictData.DirToDir.MINE_MOVE_THEIRS_EDIT ||
data == TreeConflictData.DirToDir.MINE_MOVE_THEIRS_ADD) {
change = changeListManager.getChange(VcsUtil.getFilePath(new File(myWorkingCopyDir.getPath(), conflictFile), true));
}
else {
VirtualFile vf = LocalFileSystem.getInstance().refreshAndFindFileByIoFile(new File(myWorkingCopyDir.getPath(), conflictFile));
assertNotNull(vf);
change = changeListManager.getChange(vf);
}
assertTrue(change instanceof ConflictedSvnChange);
TreeConflictDescription beforeDescription = ((ConflictedSvnChange)change).getBeforeDescription();
TreeConflictDescription afterDescription = ((ConflictedSvnChange)change).getAfterDescription();
assertNotNull(beforeDescription);
assertNull(afterDescription);
checker.accept(beforeDescription, afterDescription);
}
}
| smmribeiro/intellij-community | plugins/svn4idea/testSource/org/jetbrains/idea/svn/SvnTreeConflictDataTest.java | Java | apache-2.0 | 20,363 |
/*
* Copyright (C) 2015 The Android Open Source Project
*
* 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 com.github.retrofit2.app;
import auto.parcel.AutoParcel;
import android.os.Parcelable;
import android.support.annotation.Nullable;
@AutoParcel
public abstract class Item implements Parcelable {
@Nullable
public abstract String icon();
@Nullable
public abstract String text1();
@AutoParcel.Builder
public abstract static class Builder {
public abstract Builder icon(String s);
public abstract Builder text1(String s);
public abstract Item build();
}
public static Builder builder() {
return new AutoParcel_Item.Builder();
}
public abstract Builder toBuilder();
}
| yongjhih/NotRetrofit | retrofit2-github-app/src/main/java/com/github/retrofit2/app/Item.java | Java | apache-2.0 | 1,268 |
package org.jboss.resteasy.api.validation;
import java.io.Serializable;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlRootElement;
/**
*
* @author <a href="ron.sigal@jboss.com">Ron Sigal</a>
* @version $Revision: 1.1 $
*
* Copyright Jun 4, 2013
*/
@XmlRootElement(name="resteasyConstraintViolation")
@XmlAccessorType(XmlAccessType.FIELD)
public class ResteasyConstraintViolation implements Serializable
{
private static final long serialVersionUID = -5441628046215135260L;
private ConstraintType.Type constraintType;
private String path;
private String message;
private String value;
public ResteasyConstraintViolation(ConstraintType.Type constraintType, String path, String message, String value)
{
this.constraintType = constraintType;
this.path = path;
this.message = message;
this.value = value;
}
public ResteasyConstraintViolation()
{
}
/**
* @return type of constraint
*/
public ConstraintType.Type getConstraintType()
{
return constraintType;
}
/**
* @return description of element violating constraint
*/
public String getPath()
{
return path;
}
/**
* @return description of constraint violation
*/
public String getMessage()
{
return message;
}
/**
* @return object in violation of constraint
*/
public String getValue()
{
return value;
}
/**
* @return String representation of violation
*/
public String toString()
{
return "[" + type() + "]\r[" + path + "]\r[" + message + "]\r[" + value + "]\r";
}
/**
* @return String form of violation type
*/
public String type()
{
return constraintType.toString();
}
} | psakar/Resteasy | resteasy-jaxrs/src/main/java/org/jboss/resteasy/api/validation/ResteasyConstraintViolation.java | Java | apache-2.0 | 1,810 |
/*
* Copyright 2004-2009 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.compass.core.lucene.engine.optimizer;
import java.io.IOException;
import org.apache.lucene.index.LuceneSubIndexInfo;
import org.compass.core.engine.SearchEngineException;
import org.compass.core.lucene.engine.manager.LuceneSearchEngineIndexManager;
/**
* @author kimchy
*/
public abstract class AbstractIndexInfoOptimizer extends AbstractOptimizer {
protected void doOptimize(String subIndex) throws SearchEngineException {
LuceneSubIndexInfo indexInfo = doGetIndexInfo(subIndex);
if (indexInfo == null) {
return;
}
doOptimize(subIndex, indexInfo);
}
protected void doForceOptimize(String subIndex) throws SearchEngineException {
LuceneSubIndexInfo indexInfo = doGetIndexInfo(subIndex);
if (indexInfo == null) {
return;
}
doForceOptimize(subIndex, indexInfo);
}
protected LuceneSubIndexInfo doGetIndexInfo(String subIndex) {
LuceneSearchEngineIndexManager indexManager = getSearchEngineFactory().getLuceneIndexManager();
LuceneSubIndexInfo indexInfo;
try {
indexInfo = LuceneSubIndexInfo.getIndexInfo(subIndex, indexManager);
} catch (IOException e) {
throw new SearchEngineException("Failed to read index info for sub index [" + subIndex + "]", e);
}
if (indexInfo == null) {
// no index data, simply continue
return null;
}
if (!isRunning()) {
return null;
}
return indexInfo;
}
protected abstract void doOptimize(String subIndex, LuceneSubIndexInfo indexInfo) throws SearchEngineException;
protected abstract void doForceOptimize(String subIndex, LuceneSubIndexInfo indexInfo) throws SearchEngineException;
}
| baboune/compass | src/main/src/org/compass/core/lucene/engine/optimizer/AbstractIndexInfoOptimizer.java | Java | apache-2.0 | 2,416 |
// Copyright 2000-2021 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
package org.jetbrains.kotlin.idea.stubindex;
import com.intellij.openapi.project.Project;
import com.intellij.psi.search.GlobalSearchScope;
import com.intellij.psi.stubs.StubIndex;
import com.intellij.psi.stubs.StubIndexKey;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.kotlin.psi.KtNamedFunction;
import java.util.Collection;
/**
* Stores package top level function (both extension and non-extension) full qualified names.
*/
public class KotlinTopLevelFunctionFqnNameIndex extends AbstractStringStubIndexExtension<KtNamedFunction> {
private static final StubIndexKey<String, KtNamedFunction> KEY = KotlinIndexUtil.createIndexKey(KotlinTopLevelFunctionFqnNameIndex.class);
private static final KotlinTopLevelFunctionFqnNameIndex INSTANCE = new KotlinTopLevelFunctionFqnNameIndex();
@NotNull
public static KotlinTopLevelFunctionFqnNameIndex getInstance() {
return INSTANCE;
}
private KotlinTopLevelFunctionFqnNameIndex() {
super(KtNamedFunction.class);
}
@NotNull
@Override
public StubIndexKey<String, KtNamedFunction> getKey() {
return KEY;
}
@NotNull
@Override
public Collection<KtNamedFunction> get(@NotNull String s, @NotNull Project project, @NotNull GlobalSearchScope scope) {
return StubIndex.getElements(KEY, s, project, scope, KtNamedFunction.class);
}
// temporary hack, see comments in findCandidateDeclarationsInIndex (findDecompiledDeclaration.kt)
@NotNull
public Collection<KtNamedFunction> getNoScopeWrap(@NotNull String s, @NotNull Project project, @NotNull GlobalSearchScope scope) {
return StubIndex.getElements(KEY, s, project, scope, KtNamedFunction.class);
}
}
| smmribeiro/intellij-community | plugins/kotlin/analysis/src/org/jetbrains/kotlin/idea/stubindex/KotlinTopLevelFunctionFqnNameIndex.java | Java | apache-2.0 | 1,887 |
/*
* 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 gov.nasa.jpl.mudrod.ssearch.ranking;
import gov.nasa.jpl.mudrod.discoveryengine.MudrodAbstract;
import gov.nasa.jpl.mudrod.driver.ESDriver;
import gov.nasa.jpl.mudrod.driver.SparkDriver;
import gov.nasa.jpl.mudrod.main.MudrodConstants;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.Properties;
import static org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder;
/**
* Supports the ability to importing training set into Elasticsearch
*/
public class TrainingImporter extends MudrodAbstract {
/**
*
*/
private static final long serialVersionUID = 1L;
public TrainingImporter(Properties props, ESDriver es, SparkDriver spark) {
super(props, es, spark);
es.deleteAllByQuery(props.getProperty(MudrodConstants.ES_INDEX_NAME), "trainingranking", QueryBuilders.matchAllQuery());
addMapping();
}
/**
* Method of adding mapping to traning set type
*/
public void addMapping() {
XContentBuilder Mapping;
try {
Mapping = jsonBuilder().startObject().startObject("trainingranking").startObject("properties").startObject("query").field("type", "string").field("index", "not_analyzed").endObject()
.startObject("dataID").field("type", "string").field("index", "not_analyzed").endObject().startObject("label").field("type", "string").field("index", "not_analyzed").endObject().endObject()
.endObject().endObject();
es.getClient().admin().indices().preparePutMapping(props.getProperty("indexName")).setType("trainingranking").setSource(Mapping).execute().actionGet();
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* Method of importing training set in to Elasticsearch
*
* @param dataFolder the path to the traing set
* @throws IOException IOException
*/
public void importTrainingSet(String dataFolder) throws IOException {
es.createBulkProcessor();
File[] files = new File(dataFolder).listFiles();
for (File file : files) {
BufferedReader br = new BufferedReader(new FileReader(file.getAbsolutePath()));
br.readLine();
String line = br.readLine();
while (line != null) {
String[] list = line.split(",");
String query = file.getName().replace(".csv", "");
if (list.length > 0) {
IndexRequest ir = new IndexRequest(props.getProperty("indexName"), "trainingranking")
.source(jsonBuilder().startObject().field("query", query).field("dataID", list[0]).field("label", list[list.length - 1]).endObject());
es.getBulkProcessor().add(ir);
}
line = br.readLine();
}
br.close();
}
es.destroyBulkProcessor();
}
}
| quintinali/mudrod | core/src/main/java/gov/nasa/jpl/mudrod/ssearch/ranking/TrainingImporter.java | Java | apache-2.0 | 3,467 |
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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.apache.pdfbox.text;
import java.io.InputStream;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.pdfbox.contentstream.PDFStreamEngine;
import org.apache.pdfbox.pdmodel.PDPage;
import org.apache.pdfbox.pdmodel.font.encoding.GlyphList;
import org.apache.pdfbox.pdmodel.common.PDRectangle;
import org.apache.pdfbox.pdmodel.font.PDFont;
import org.apache.pdfbox.pdmodel.font.PDSimpleFont;
import org.apache.pdfbox.pdmodel.font.PDType3Font;
import org.apache.pdfbox.pdmodel.graphics.state.PDGraphicsState;
import java.io.IOException;
import org.apache.pdfbox.util.Matrix;
import org.apache.pdfbox.util.Vector;
import org.apache.pdfbox.contentstream.operator.DrawObject;
import org.apache.pdfbox.contentstream.operator.state.Concatenate;
import org.apache.pdfbox.contentstream.operator.state.Restore;
import org.apache.pdfbox.contentstream.operator.state.Save;
import org.apache.pdfbox.contentstream.operator.state.SetGraphicsStateParameters;
import org.apache.pdfbox.contentstream.operator.state.SetMatrix;
import org.apache.pdfbox.contentstream.operator.text.BeginText;
import org.apache.pdfbox.contentstream.operator.text.EndText;
import org.apache.pdfbox.contentstream.operator.text.SetFontAndSize;
import org.apache.pdfbox.contentstream.operator.text.SetTextHorizontalScaling;
import org.apache.pdfbox.contentstream.operator.text.ShowTextAdjusted;
import org.apache.pdfbox.contentstream.operator.text.ShowTextLine;
import org.apache.pdfbox.contentstream.operator.text.ShowTextLineAndSpace;
import org.apache.pdfbox.contentstream.operator.text.MoveText;
import org.apache.pdfbox.contentstream.operator.text.MoveTextSetLeading;
import org.apache.pdfbox.contentstream.operator.text.NextLine;
import org.apache.pdfbox.contentstream.operator.text.SetCharSpacing;
import org.apache.pdfbox.contentstream.operator.text.SetTextLeading;
import org.apache.pdfbox.contentstream.operator.text.SetTextRenderingMode;
import org.apache.pdfbox.contentstream.operator.text.SetTextRise;
import org.apache.pdfbox.contentstream.operator.text.SetWordSpacing;
import org.apache.pdfbox.contentstream.operator.text.ShowText;
/**
* PDFStreamEngine subclass for advanced processing of text via TextPosition.
*
* @see org.apache.pdfbox.text.TextPosition
* @author Ben Litchfield
* @author John Hewson
*/
class PDFTextStreamEngine extends PDFStreamEngine
{
private static final Log LOG = LogFactory.getLog(PDFTextStreamEngine.class);
private int pageRotation;
private PDRectangle pageSize;
private final GlyphList glyphList;
/**
* Constructor.
*/
PDFTextStreamEngine() throws IOException
{
addOperator(new BeginText());
addOperator(new Concatenate());
addOperator(new DrawObject()); // special text version
addOperator(new EndText());
addOperator(new SetGraphicsStateParameters());
addOperator(new Save());
addOperator(new Restore());
addOperator(new NextLine());
addOperator(new SetCharSpacing());
addOperator(new MoveText());
addOperator(new MoveTextSetLeading());
addOperator(new SetFontAndSize());
addOperator(new ShowText());
addOperator(new ShowTextAdjusted());
addOperator(new SetTextLeading());
addOperator(new SetMatrix());
addOperator(new SetTextRenderingMode());
addOperator(new SetTextRise());
addOperator(new SetWordSpacing());
addOperator(new SetTextHorizontalScaling());
addOperator(new ShowTextLine());
addOperator(new ShowTextLineAndSpace());
// load additional glyph list for Unicode mapping
String path = "org/apache/pdfbox/resources/glyphlist/additional.txt";
InputStream input = GlyphList.class.getClassLoader().getResourceAsStream(path);
glyphList = new GlyphList(GlyphList.getAdobeGlyphList(), input);
}
/**
* This will initialise and process the contents of the stream.
*
* @param page the page to process
* @throws java.io.IOException if there is an error accessing the stream.
*/
@Override
public void processPage(PDPage page) throws IOException
{
this.pageRotation = page.getRotation();
this.pageSize = page.getCropBox();
super.processPage(page);
}
/**
* This method was originally written by Ben Litchfield for PDFStreamEngine.
*/
@Override
protected void showGlyph(Matrix textRenderingMatrix, PDFont font, int code, String unicode,
Vector displacement) throws IOException
{
//
// legacy calculations which were previously in PDFStreamEngine
//
PDGraphicsState state = getGraphicsState();
Matrix ctm = state.getCurrentTransformationMatrix();
float fontSize = state.getTextState().getFontSize();
float horizontalScaling = state.getTextState().getHorizontalScaling() / 100f;
Matrix textMatrix = getTextMatrix();
// 1/2 the bbox is used as the height todo: why?
float glyphHeight = font.getBoundingBox().getHeight() / 2;
// transformPoint from glyph space -> text space
float height = font.getFontMatrix().transformPoint(0, glyphHeight).y;
// (modified) combined displacement, this is calculated *without* taking the character
// spacing and word spacing into account, due to legacy code in TextStripper
float tx = displacement.getX() * fontSize * horizontalScaling;
float ty = 0; // todo: support vertical writing mode
// (modified) combined displacement matrix
Matrix td = Matrix.getTranslateInstance(tx, ty);
// (modified) text rendering matrix
Matrix nextTextRenderingMatrix = td.multiply(textMatrix).multiply(ctm); // text space -> device space
float nextX = nextTextRenderingMatrix.getTranslateX();
float nextY = nextTextRenderingMatrix.getTranslateY();
// (modified) width and height calculations
float dxDisplay = nextX - textRenderingMatrix.getTranslateX();
float dyDisplay = height * textRenderingMatrix.getScalingFactorY();
//
// start of the original method
//
// Note on variable names. There are three different units being used in this code.
// Character sizes are given in glyph units, text locations are initially given in text
// units, and we want to save the data in display units. The variable names should end with
// Text or Disp to represent if the values are in text or disp units (no glyph units are
// saved).
float fontSizeText = getGraphicsState().getTextState().getFontSize();
float horizontalScalingText = getGraphicsState().getTextState().getHorizontalScaling()/100f;
//Matrix ctm = getGraphicsState().getCurrentTransformationMatrix();
float glyphSpaceToTextSpaceFactor = 1 / 1000f;
if (font instanceof PDType3Font)
{
// This will typically be 1000 but in the case of a type3 font
// this might be a different number
glyphSpaceToTextSpaceFactor = 1f / font.getFontMatrix().getScaleX();
}
float spaceWidthText = 0;
try
{
// to avoid crash as described in PDFBOX-614, see what the space displacement should be
spaceWidthText = font.getSpaceWidth() * glyphSpaceToTextSpaceFactor;
}
catch (Throwable exception)
{
LOG.warn(exception, exception);
}
if (spaceWidthText == 0)
{
spaceWidthText = font.getAverageFontWidth() * glyphSpaceToTextSpaceFactor;
// the average space width appears to be higher than necessary so make it smaller
spaceWidthText *= .80f;
}
if (spaceWidthText == 0)
{
spaceWidthText = 1.0f; // if could not find font, use a generic value
}
// the space width has to be transformed into display units
float spaceWidthDisplay = spaceWidthText * fontSizeText * horizontalScalingText *
textRenderingMatrix.getScalingFactorX() * ctm.getScalingFactorX();
// use our additional glyph list for Unicode mapping
unicode = font.toUnicode(code, glyphList);
// when there is no Unicode mapping available, Acrobat simply coerces the character code
// into Unicode, so we do the same. Subclasses of PDFStreamEngine don't necessarily want
// this, which is why we leave it until this point in PDFTextStreamEngine.
if (unicode == null)
{
if (font instanceof PDSimpleFont)
{
char c = (char) code;
unicode = new String(new char[] { c });
}
else
{
// Acrobat doesn't seem to coerce composite font's character codes, instead it
// skips them. See the "allah2.pdf" TestTextStripper file.
return;
}
}
processTextPosition(new TextPosition(pageRotation, pageSize.getWidth(),
pageSize.getHeight(), textRenderingMatrix, nextX, nextY,
dyDisplay, dxDisplay,
spaceWidthDisplay, unicode, new int[] { code } , font, fontSize,
(int)(fontSize * textRenderingMatrix.getScalingFactorX())));
}
/**
* A method provided as an event interface to allow a subclass to perform some specific
* functionality when text needs to be processed.
*
* @param text The text to be processed.
*/
protected void processTextPosition(TextPosition text)
{
// subclasses can override to provide specific functionality
}
}
| ZhenyaM/veraPDF-pdfbox | pdfbox/src/main/java/org/apache/pdfbox/text/PDFTextStreamEngine.java | Java | apache-2.0 | 10,595 |
/**
* Copyright 2009 Google Inc.
*
* 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.waveprotocol.wave.model.supplement;
import org.waveprotocol.wave.model.id.WaveletId;
import org.waveprotocol.wave.model.wave.SourcesEvents;
/**
*/
public interface ObservablePrimitiveSupplement extends PrimitiveSupplement,
SourcesEvents<ObservablePrimitiveSupplement.Listener> {
public interface Listener {
/**
* Notifies this listener that the last-read version of a blip has changed.
*/
void onLastReadBlipVersionChanged(WaveletId wid, String bid, int oldVersion, int newVersion);
/**
* Notifies this listener that the minimum last-read version of all wave
* parts has changed.
*/
void onLastReadWaveletVersionChanged(WaveletId wid, int oldVersion, int newVersion);
/**
* Notifies this listener that the last-read version of the
* participants-collection has changed.
*/
void onLastReadParticipantsVersionChanged(WaveletId wid, int oldVersion, int newVersion);
/**
* Notifies this listener that the last-read version of the tags has
* changed.
*/
void onLastReadTagsVersionChanged(WaveletId wid, int oldVersion, int newVersion);
/**
* Notifies this listener that the followed state has been set to true.
*/
void onFollowed();
/**
* Notifies this listener that the followed state has been set to false.
*/
void onUnfollowed();
/**
* Notifies this listener that the followed state has been cleared.
*/
void onFollowCleared();
/**
* Notifies this listener that last-archived version of a wavelet has
* changed.
*/
void onArchiveVersionChanged(WaveletId wid, int oldVersion, int newVersion);
/**
* Notifies this listener that archive value has been set.
*/
// TODO(hearnden/fabio) remove the 'cleared' field from the primitive model
void onArchiveClearChanged(boolean oldValue, boolean newValue);
/**
* Notifies this listener that a folder id has been added.
*/
void onFolderAdded(int newFolder);
/**
* Notifies this listener that a folder id has been removed.
*/
void onFolderRemoved(int oldFolder);
/**
* Notifies this listener that the wanted-evaluations of a wavelet has
* changed.
*/
void onWantedEvaluationsChanged(WaveletId wid);
/**
* Notifies this listener that a thread's state has been changed
* ThreadState values shall never be null.
*/
void onThreadStateChanged(WaveletId wid, String tid,
ThreadState oldState, ThreadState newState);
/**
* Notifies this listener that gadget state has been changed.
*/
void onGadgetStateChanged(String gadgetId, String key, String oldValue, String newValue);
}
}
| JaredMiller/Wave | src/org/waveprotocol/wave/model/supplement/ObservablePrimitiveSupplement.java | Java | apache-2.0 | 3,341 |
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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 com.taobao.weex.dom;
import android.support.v4.util.ArrayMap;
import com.alibaba.fastjson.JSONObject;
import com.taobao.weex.dom.binding.ELUtils;
import com.taobao.weex.dom.binding.JSONUtils;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
/**
* Store value of component event
*/
public class WXEvent extends ArrayList<String> implements Serializable, Cloneable {
private static final long serialVersionUID = -8186587029452440107L;
/**
* event data format
* {
* type: 'appear',
* params: [
* { '@binding': 'index' },
* 'static',
* { '@binding': 'item.name' },
* { '@binding': '$event' }
* ]
* }
* */
public static final String EVENT_KEY_TYPE = "type";
public static final String EVENT_KEY_ARGS = "params";
/**
* dynamic binding event args, can be null, only weex use
* */
private ArrayMap mEventBindingArgs;
private ArrayMap<String, List<Object>> mEventBindingArgsValues;
@Override
public void clear() {
if(mEventBindingArgs != null){
mEventBindingArgs.clear();
}
if(mEventBindingArgsValues != null){
mEventBindingArgsValues.clear();
}
super.clear();
}
public boolean remove(String o) {
if(mEventBindingArgs != null){
mEventBindingArgs.remove(o);
}
if(mEventBindingArgsValues != null){
mEventBindingArgsValues.remove(o);
}
return super.remove(o);
}
/**
* can by null
* */
public ArrayMap getEventBindingArgs() {
return mEventBindingArgs;
}
public ArrayMap<String, List<Object>> getEventBindingArgsValues() {
return mEventBindingArgsValues;
}
public void addEvent(Object event) {
if(event instanceof CharSequence){
if(JSONUtils.isJSON(event.toString())){
addEvent(JSONUtils.toJSON(event.toString()));
return;
}
String eventName = event.toString();
if(!contains(eventName)){
add(eventName);
}
}else if(event instanceof JSONObject){
JSONObject bindings = (JSONObject) event;
addBindingEvent(bindings);
}
}
public static String getEventName(Object event){
if(event instanceof CharSequence){
return event.toString();
}else if(event instanceof JSONObject){
JSONObject bindings = (JSONObject) event;
String eventName = bindings.getString(WXEvent.EVENT_KEY_TYPE);
return eventName;
}
if(event == null){
return null;
}
return event.toString();
}
public void parseStatements() {
if(!isEmpty()){
for(int i=0; i<size(); i++){
String event = get(i);
if(JSONUtils.isJSON(event)){
JSONObject object = JSONUtils.toJSON(event);
String eventName = addBindingEvent(object);
set(i, eventName);
}
}
}
}
private String addBindingEvent(JSONObject bindings){
String eventName = bindings.getString(WXEvent.EVENT_KEY_TYPE);
Object args = bindings.get(WXEvent.EVENT_KEY_ARGS);
if (eventName != null) {
addBindingArgsEvent(eventName, args);
}
return eventName;
}
private void addBindingArgsEvent(String eventName, Object args){
if(!contains(eventName)){
add(eventName);
}
if(args != null){
if(mEventBindingArgs == null){
mEventBindingArgs = new ArrayMap();
}
mEventBindingArgs.put(eventName, ELUtils.bindingBlock(args));
}
}
public void putEventBindingArgsValue(String event, List<Object> value){
if(mEventBindingArgsValues == null){
mEventBindingArgsValues = new ArrayMap();
}
if(value == null){
mEventBindingArgsValues.remove(event);
}else{
mEventBindingArgsValues.put(event, value);
}
}
@Override
public WXEvent clone() {
WXEvent event = new WXEvent();
event.addAll(this);
if(mEventBindingArgs != null) {
event.mEventBindingArgs = new ArrayMap(mEventBindingArgs);
}
event.mEventBindingArgsValues = null; //this should not be clone, it dynamic args
return event;
}
}
| erha19/incubator-weex | android/sdk/src/main/java/com/taobao/weex/dom/WXEvent.java | Java | apache-2.0 | 4,905 |
package org.jb2011.lnf.beautyeye.winlnfutils.d;
///*
// * @(#)XPStyle.java 1.28 07/01/09
// *
// * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
// * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
// */
//
///*
// * <p>These classes are designed to be used while the
// * corresponding <code>LookAndFeel</code> class has been installed
// * (<code>UIManager.setLookAndFeel(new <i>XXX</i>LookAndFeel())</code>).
// * Using them while a different <code>LookAndFeel</code> is installed
// * may produce unexpected results, including exceptions.
// * Additionally, changing the <code>LookAndFeel</code>
// * maintained by the <code>UIManager</code> without updating the
// * corresponding <code>ComponentUI</code> of any
// * <code>JComponent</code>s may also produce unexpected results,
// * such as the wrong colors showing up, and is generally not
// * encouraged.
// *
// */
//
//package org.jb2011.lnf.beautyeye.winlnfutils;
//
//import java.awt.Color;
//import java.awt.Component;
//import java.awt.Dimension;
//import java.awt.Graphics;
//import java.awt.GraphicsConfiguration;
//import java.awt.Image;
//import java.awt.Insets;
//import java.awt.Point;
//import java.awt.Rectangle;
//import java.awt.Toolkit;
//import java.awt.image.BufferedImage;
//import java.awt.image.DataBufferInt;
//import java.awt.image.WritableRaster;
//import java.security.AccessController;
//import java.util.HashMap;
//
//import javax.swing.AbstractButton;
//import javax.swing.JButton;
//import javax.swing.JCheckBox;
//import javax.swing.JRadioButton;
//import javax.swing.JToolBar;
//import javax.swing.UIManager;
//import javax.swing.border.AbstractBorder;
//import javax.swing.border.Border;
//import javax.swing.border.EmptyBorder;
//import javax.swing.border.LineBorder;
//import javax.swing.plaf.ColorUIResource;
//import javax.swing.plaf.InsetsUIResource;
//import javax.swing.plaf.UIResource;
//import javax.swing.text.JTextComponent;
//
//import org.jb2011.lnf.beautyeye.winlnfutils.BETMSchema.Part;
//import org.jb2011.lnf.beautyeye.winlnfutils.BETMSchema.Prop;
//import org.jb2011.lnf.beautyeye.winlnfutils.BETMSchema.State;
//import org.jb2011.lnf.beautyeye.winlnfutils.BETMSchema.TypeEnum;
//
//import sun.awt.windows.ThemeReader;
//import sun.security.action.GetPropertyAction;
//import sun.swing.CachedPainter;
//
//import com.sun.java.swing.plaf.windows.WindowsClassicLookAndFeel;
//
///*
// * 本类实际就是XP主题包中的类,未作任何改变.
// * 代码参考java源码,仅作兼容性修改
// * Add by js 2009-09-01.
// */
///**
// * Implements Windows XP Styles for the Windows Look and Feel.
// *
// * @version 1.28 01/09/07
// * @author Leif Samuelsson
// */
//public class BEXPStyle {
// // Singleton instance of this class
// private static BEXPStyle xp;
//
// // Singleton instance of SkinPainter
// private static SkinPainter skinPainter = new SkinPainter();
//
// private static Boolean themeActive = null;
//
// private HashMap<String, Border> borderMap;
// private HashMap<String, Color> colorMap;
//
// private boolean flatMenus;
//
// static {
// invalidateStyle();
// }
//
// /** Static method for clearing the hashmap and loading the
// * current XP style and theme
// */
// static synchronized void invalidateStyle() {
// xp = null;
// themeActive = null;
// }
//
// /** Get the singleton instance of this class
// *
// * @return the singleton instance of this class or null if XP styles
// * are not active or if this is not Windows XP
// */
// public static synchronized BEXPStyle getXP() {
// if (themeActive == null) {
// Toolkit toolkit = Toolkit.getDefaultToolkit();
// themeActive =
// (Boolean)toolkit.getDesktopProperty("win.xpstyle.themeActive");
// if (themeActive == null) {
// themeActive = Boolean.FALSE;
// }
// if (themeActive.booleanValue()) {
// GetPropertyAction propertyAction =
// new GetPropertyAction("swing.noxp");
// if (AccessController.doPrivileged(propertyAction) == null &&
// ThemeReader.isThemed() &&
// !(UIManager.getLookAndFeel()
// instanceof WindowsClassicLookAndFeel)) {
//
// xp = new BEXPStyle();
// }
// }
// }
// return xp;
// }
//
//
//
// /** Get a named <code>String</code> value from the current style
// *
// * @param part a <code>Part</code>
// * @param state a <code>String</code>
// * @param attributeKey a <code>String</code>
// * @return a <code>String</code> or null if key is not found
// * in the current style
// *
// * This is currently only used by WindowsInternalFrameTitlePane for painting
// * title foregound and can be removed when no longer needed
// */
// String getString(Component c, Part part, State state, Prop prop) {
// return getTypeEnumName(c, part, state, prop);
// }
//
// private static String getTypeEnumName(Component c, Part part, State state, Prop prop) {
// int enumValue = ThemeReader.getEnum(part.getControlName(c), part.getValue(),
// State.getValue(part, state),
// prop.getValue());
// if (enumValue == -1) {
// return null;
// }
// return TypeEnum.getTypeEnum(prop, enumValue).getName();
// }
//
//
//
//
// /** Get a named <code>int</code> value from the current style
// *
// * @param part a <code>Part</code>
// * @return an <code>int</code> or null if key is not found
// * in the current style
// */
// int getInt(Component c, Part part, State state, Prop prop, int fallback) {
// return ThemeReader.getInt(part.getControlName(c), part.getValue(),
// State.getValue(part, state),
// prop.getValue());
// }
//
// /** Get a named <code>Dimension</code> value from the current style
// *
// * @param key a <code>String</code>
// * @return a <code>Dimension</code> or null if key is not found
// * in the current style
// *
// * This is currently only used by WindowsProgressBarUI and the value
// * should probably be cached there instead of here.
// */
// Dimension getDimension(Component c, Part part, State state, Prop prop) {
// return ThemeReader.getPosition(part.getControlName(c), part.getValue(),
// State.getValue(part, state),
// prop.getValue());
// }
//
// /** Get a named <code>Point</code> (e.g. a location or an offset) value
// * from the current style
// *
// * @param key a <code>String</code>
// * @return a <code>Point</code> or null if key is not found
// * in the current style
// *
// * This is currently only used by WindowsInternalFrameTitlePane for painting
// * title foregound and can be removed when no longer needed
// */
// Point getPoint(Component c, Part part, State state, Prop prop) {
// Dimension d = ThemeReader.getPosition(part.getControlName(c), part.getValue(),
// State.getValue(part, state),
// prop.getValue());
// if (d != null) {
// return new Point(d.width, d.height);
// } else {
// return null;
// }
// }
//
// /** Get a named <code>Insets</code> value from the current style
// *
// * @param key a <code>String</code>
// * @return an <code>Insets</code> object or null if key is not found
// * in the current style
// *
// * This is currently only used to create borders and by
// * WindowsInternalFrameTitlePane for painting title foregound.
// * The return value is already cached in those places.
// */
// Insets getMargin(Component c, Part part, State state, Prop prop) {
// return ThemeReader.getThemeMargins(part.getControlName(c), part.getValue(),
// State.getValue(part, state),
// prop.getValue());
// }
//
//
// /** Get a named <code>Color</code> value from the current style
// *
// * @param part a <code>Part</code>
// * @return a <code>Color</code> or null if key is not found
// * in the current style
// */
// synchronized Color getColor(Skin skin, Prop prop, Color fallback) {
// String key = skin.toString() + "." + prop.name();
// Part part = skin.part;
// Color color = colorMap.get(key);
// if (color == null) {
// color = ThemeReader.getColor(part.getControlName(null), part.getValue(),
// State.getValue(part, skin.state),
// prop.getValue());
// if (color != null) {
// color = new ColorUIResource(color);
// colorMap.put(key, color);
// }
// }
// return (color != null) ? color : fallback;
// }
//
// public Color getColor(Component c, Part part, State state, Prop prop, Color fallback) {
// return getColor(new Skin(c, part, state), prop, fallback);
// }
//
//
//
// /** Get a named <code>Border</code> value from the current style
// *
// * @param part a <code>Part</code>
// * @return a <code>Border</code> or null if key is not found
// * in the current style or if the style for the particular
// * part is not defined as "borderfill".
// */
// public synchronized Border getBorder(Component c, Part part) {
// if (part == Part.MENU) {
// // Special case because XP has no skin for menus
// if (flatMenus) {
// // TODO: The classic border uses this color, but we should
// // create a new UI property called "PopupMenu.borderColor"
// // instead.
// return new XPFillBorder(UIManager.getColor("InternalFrame.borderShadow"),
// 1);
// } else {
// return null; // Will cause L&F to use classic border
// }
// }
// Skin skin = new Skin(c, part, null);
// Border border = borderMap.get(skin.string);
// if (border == null) {
// String bgType = getTypeEnumName(c, part, null, Prop.BGTYPE);
// if ("borderfill".equalsIgnoreCase(bgType)) {
// int thickness = getInt(c, part, null, Prop.BORDERSIZE, 1);
// Color color = getColor(skin, Prop.BORDERCOLOR, Color.black);
// border = new XPFillBorder(color, thickness);
// } else if ("imagefile".equalsIgnoreCase(bgType)) {
// Insets m = getMargin(c, part, null, Prop.SIZINGMARGINS);
// if (m != null) {
// if (getBoolean(c, part, null, Prop.BORDERONLY)) {
// border = new XPImageBorder(c, part);
// } else {
// if(part == Part.TP_BUTTON) {
// border = new XPEmptyBorder(new Insets(3,3,3,3));
// } else {
// border = new XPEmptyBorder(m);
// }
// }
// }
// }
// if (border != null) {
// borderMap.put(skin.string, border);
// }
// }
// return border;
// }
//
// private class XPFillBorder extends LineBorder implements UIResource {
// XPFillBorder(Color color, int thickness) {
// super(color, thickness);
// }
//
// public Insets getBorderInsets(Component c) {
// return getBorderInsets(c, new Insets(0,0,0,0));
// }
//
// public Insets getBorderInsets(Component c, Insets insets) {
// Insets margin = null;
// //
// // Ideally we'd have an interface defined for classes which
// // support margins (to avoid this hackery), but we've
// // decided against it for simplicity
// //
// if (c instanceof AbstractButton) {
// margin = ((AbstractButton)c).getMargin();
// } else if (c instanceof JToolBar) {
// margin = ((JToolBar)c).getMargin();
// } else if (c instanceof JTextComponent) {
// margin = ((JTextComponent)c).getMargin();
// }
// insets.top = (margin != null? margin.top : 0) + thickness;
// insets.left = (margin != null? margin.left : 0) + thickness;
// insets.bottom = (margin != null? margin.bottom : 0) + thickness;
// insets.right = (margin != null? margin.right : 0) + thickness;
//
// return insets;
// }
// }
//
// private class XPImageBorder extends AbstractBorder implements UIResource {
// Skin skin;
//
// XPImageBorder(Component c, Part part) {
// this.skin = getSkin(c, part);
// }
//
// public void paintBorder(Component c, Graphics g,
// int x, int y, int width, int height) {
// skin.paintSkin(g, x, y, width, height, null);
// }
//
// public Insets getBorderInsets(Component c) {
// return getBorderInsets(c, new Insets(0,0,0,0));
// }
//
// public Insets getBorderInsets(Component c, Insets insets) {
// Insets margin = null;
// Insets borderInsets = skin.getContentMargin();
// //
// // Ideally we'd have an interface defined for classes which
// // support margins (to avoid this hackery), but we've
// // decided against it for simplicity
// //
// if (c instanceof AbstractButton) {
// margin = ((AbstractButton)c).getMargin();
// } else if (c instanceof JToolBar) {
// margin = ((JToolBar)c).getMargin();
// } else if (c instanceof JTextComponent) {
// margin = ((JTextComponent)c).getMargin();
// }
// insets.top = (margin != null? margin.top : 0) + borderInsets.top;
// insets.left = (margin != null? margin.left : 0) + borderInsets.left;
// insets.bottom = (margin != null? margin.bottom : 0) + borderInsets.bottom;
// insets.right = (margin != null? margin.right : 0) + borderInsets.right;
//
// return insets;
// }
// }
//
// private class XPEmptyBorder extends EmptyBorder implements UIResource {
// XPEmptyBorder(Insets m) {
// super(m.top+2, m.left+2, m.bottom+2, m.right+2);
// }
//
// public Insets getBorderInsets(Component c) {
// return getBorderInsets(c, getBorderInsets());
// }
//
// public Insets getBorderInsets(Component c, Insets insets) {
// insets = super.getBorderInsets(c, insets);
//
// Insets margin = null;
// if (c instanceof AbstractButton) {
// Insets m = ((AbstractButton)c).getMargin();
// // if this is a toolbar button then ignore getMargin()
// // and subtract the padding added by the constructor
// if(c.getParent() instanceof JToolBar
// && ! (c instanceof JRadioButton)
// && ! (c instanceof JCheckBox)
// && m instanceof InsetsUIResource) {
// insets.top -= 2;
// insets.left -= 2;
// insets.bottom -= 2;
// insets.right -= 2;
// } else {
// margin = m;
// }
// } else if (c instanceof JToolBar) {
// margin = ((JToolBar)c).getMargin();
// } else if (c instanceof JTextComponent) {
// margin = ((JTextComponent)c).getMargin();
// }
// if (margin != null) {
// insets.top = margin.top + 2;
// insets.left = margin.left + 2;
// insets.bottom = margin.bottom + 2;
// insets.right = margin.right + 2;
// }
// return insets;
// }
// }
//
// public boolean isSkinDefined(Component c, Part part) {
// return (part.getValue() == 0)
// || ThemeReader.isThemePartDefined(
// part.getControlName(c), part.getValue(), 0);
// }
//
// /** Get a <code>Skin</code> object from the current style
// * for a named part (component type)
// *
// * @param part a <code>Part</code>
// * @return a <code>Skin</code> object
// */
// public synchronized Skin getSkin(Component c, Part part) {
// assert isSkinDefined(c, part) : "part " + part + " is not defined";
// return new Skin(c, part, null);
// }
//
//
//
//
// /** A class which encapsulates attributes for a given part
// * (component type) and which provides methods for painting backgrounds
// * and glyphs
// */
// public static class Skin {
// final Component component;
// final Part part;
// final State state;
//
// private final String string;
// private Dimension size = null;
//
// Skin(Component component, Part part) {
// this(component, part, null);
// }
//
// Skin(Part part, State state) {
// this(null, part, state);
// }
//
// Skin(Component component, Part part, State state) {
// this.component = component;
// this.part = part;
// this.state = state;
//
// String str = part.getControlName(component) +"." + part.name();
// if (state != null) {
// str += "("+state.name()+")";
// }
// string = str;
// }
//
// public Insets getContentMargin() {
// // This is only called by WindowsTableHeaderUI so far.
// return ThemeReader.getThemeMargins(part.getControlName(null), part.getValue(),
// 0, Prop.SIZINGMARGINS.getValue());
// }
//
// private int getWidth(State state) {
// if (size == null) {
// size = getPartSize(part, state);
// }
// return size.width;
// }
//
// public int getWidth() {
// return getWidth((state != null) ? state : State.NORMAL);
// }
//
// private int getHeight(State state) {
// if (size == null) {
// size = getPartSize(part, state);
// }
// return size.height;
// }
//
// public int getHeight() {
// return getHeight((state != null) ? state : State.NORMAL);
// }
//
// public String toString() {
// return string;
// }
//
// public boolean equals(Object obj) {
// return (obj instanceof Skin && ((Skin)obj).string.equals(string));
// }
//
// public int hashCode() {
// return string.hashCode();
// }
//
// /** Paint a skin at x, y.
// *
// * @param g the graphics context to use for painting
// * @param dx the destination <i>x</i> coordinate.
// * @param dy the destination <i>y</i> coordinate.
// * @param state which state to paint
// */
// public void paintSkin(Graphics g, int dx, int dy, State state) {
// if (state == null) {
// state = this.state;
// }
// paintSkin(g, dx, dy, getWidth(state), getHeight(state), state);
// }
//
// /** Paint a skin in an area defined by a rectangle.
// *
// * @param g the graphics context to use for painting
// * @param r a <code>Rectangle</code> defining the area to fill,
// * may cause the image to be stretched or tiled
// * @param state which state to paint
// */
// void paintSkin(Graphics g, Rectangle r, State state) {
// paintSkin(g, r.x, r.y, r.width, r.height, state);
// }
//
// /** Paint a skin at a defined position and size
// *
// * @param g the graphics context to use for painting
// * @param dx the destination <i>x</i> coordinate.
// * @param dy the destination <i>y</i> coordinate.
// * @param dw the width of the area to fill, may cause
// * the image to be stretched or tiled
// * @param dh the height of the area to fill, may cause
// * the image to be stretched or tiled
// * @param state which state to paint
// */
// public void paintSkin(Graphics g, int dx, int dy, int dw, int dh, State state) {
// skinPainter.paint(null, g, dx, dy, dw, dh, this, state);
// }
// /**
// * Paint a skin at a defined position and size
// *
// * @param g the graphics context to use for painting
// * @param dx the destination <i>x</i> coordinate
// * @param dy the destination <i>y</i> coordinate
// * @param dw the width of the area to fill, may cause
// * the image to be stretched or tiled
// * @param dh the height of the area to fill, may cause
// * the image to be stretched or tiled
// * @param state which state to paint
// * @param borderFill should test if the component uses a border fill
// * and skip painting if it is
// */
// void paintSkin(Graphics g, int dx, int dy, int dw, int dh, State state,
// boolean borderFill) {
// if(borderFill && "borderfill".equals(getTypeEnumName(component, part,
// state, Prop.BGTYPE))) {
// return;
// }
// skinPainter.paint(null, g, dx, dy, dw, dh, this, state);
// }
// }
//
// private static class SkinPainter extends CachedPainter {
// SkinPainter() {
// super(30);
// flush();
// }
//
// protected void paintToImage(Component c, Image image, Graphics g,
// int w, int h, Object[] args) {
// Skin skin = (Skin)args[0];
// Part part = skin.part;
// State state = (State)args[1];
// if (state == null) {
// state = skin.state;
// }
// if (c == null) {
// c = skin.component;
// }
// WritableRaster raster = ((BufferedImage)image).getRaster();
// DataBufferInt buffer = (DataBufferInt)raster.getDataBuffer();
// ThemeReader.paintBackground(buffer.getData(),
// part.getControlName(c), part.getValue(),
// State.getValue(part, state),
// 0, 0, w, h, w);
// }
//
// protected Image createImage(Component c, int w, int h,
// GraphicsConfiguration config, Object[] args) {
// return new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);
// }
// }
//
// static class GlyphButton extends JButton {
// private Skin skin;
//
// public GlyphButton(Component parent, Part part) {
// BEXPStyle xp = getXP();
// skin = xp.getSkin(parent, part);
// setBorder(null);
// setContentAreaFilled(false);
// }
//
// public boolean isFocusTraversable() {
// return false;
// }
//
// protected State getState() {
// State state = State.NORMAL;
// if (!isEnabled()) {
// state = State.DISABLED;
// } else if (getModel().isPressed()) {
// state = State.PRESSED;
// } else if (getModel().isRollover()) {
// state = State.HOT;
// }
// return state;
// }
//
// public void paintComponent(Graphics g) {
// Dimension d = getSize();
// skin.paintSkin(g, 0, 0, d.width, d.height, getState());
// }
//
// public void setPart(Component parent, Part part) {
// BEXPStyle xp = getXP();
// skin = xp.getSkin(parent, part);
// revalidate();
// repaint();
// }
//
// protected void paintBorder(Graphics g) {
// }
//
// public Dimension getPreferredSize() {
// return new Dimension(16, 16);
// }
//
// public Dimension getMinimumSize() {
// return new Dimension(5, 5);
// }
//
// public Dimension getMaximumSize() {
// return new Dimension(Integer.MAX_VALUE, Integer.MAX_VALUE);
// }
// }
//
// // Private constructor
// private BEXPStyle() {
// flatMenus = getSysBoolean(Prop.FLATMENUS);
//
// colorMap = new HashMap<String, Color>();
// borderMap = new HashMap<String, Border>();
// // Note: All further access to the maps must be synchronized
// }
//
//
// private boolean getBoolean(Component c, Part part, State state, Prop prop) {
// return ThemeReader.getBoolean(part.getControlName(c), part.getValue(),
// State.getValue(part, state),
// prop.getValue());
// }
//
// private static Dimension getPartSize(Part part, State state) {
// return ThemeReader.getPartSize(part.getControlName(null), part.getValue(),
// State.getValue(part, state));
// }
//
// private static boolean getSysBoolean(Prop prop) {
// // We can use any widget name here, I guess.
// return ThemeReader.getSysBoolean("window", prop.getValue());
// }
//}
| JackJiang2011/beautyeye | src_all/beautyeye_v3.7_utf8/src/org/jb2011/lnf/beautyeye/winlnfutils/d/BEXPStyle.java | Java | apache-2.0 | 26,390 |
/*
* Licensed to the Apache Software Foundation (ASF) under one or more contributor license
* agreements. See the NOTICE file distributed with this work for additional information regarding
* copyright ownership. The ASF licenses this file to You 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.apache.geode.redis.internal.executor.transactions;
import io.netty.buffer.ByteBuf;
import java.util.Queue;
import org.apache.geode.cache.CacheTransactionManager;
import org.apache.geode.cache.CommitConflictException;
import org.apache.geode.cache.TransactionId;
import org.apache.geode.redis.internal.Coder;
import org.apache.geode.redis.internal.Command;
import org.apache.geode.redis.internal.ExecutionHandlerContext;
import org.apache.geode.redis.internal.RedisConstants;
public class ExecExecutor extends TransactionExecutor {
@Override
public void executeCommand(Command command, ExecutionHandlerContext context) {
CacheTransactionManager txm = context.getCacheTransactionManager();
if (!context.hasTransaction()) {
command.setResponse(Coder.getNilResponse(context.getByteBufAllocator()));
return;
}
TransactionId transactionId = context.getTransactionID();
txm.resume(transactionId);
boolean hasError = hasError(context.getTransactionQueue());
if (hasError)
txm.rollback();
else {
try {
txm.commit();
} catch (CommitConflictException e) {
command.setResponse(Coder.getErrorResponse(context.getByteBufAllocator(),
RedisConstants.ERROR_COMMIT_CONFLICT));
context.clearTransaction();
return;
}
}
ByteBuf response = constructResponseExec(context);
command.setResponse(response);
context.clearTransaction();
}
private ByteBuf constructResponseExec(ExecutionHandlerContext context) {
Queue<Command> cQ = context.getTransactionQueue();
ByteBuf response = context.getByteBufAllocator().buffer();
response.writeByte(Coder.ARRAY_ID);
response.writeBytes(Coder.intToBytes(cQ.size()));
response.writeBytes(Coder.CRLFar);
for (Command c : cQ) {
ByteBuf r = c.getResponse();
response.writeBytes(r);
}
return response;
}
private boolean hasError(Queue<Command> queue) {
for (Command c : queue) {
if (c.hasError())
return true;
}
return false;
}
}
| charliemblack/geode | geode-core/src/main/java/org/apache/geode/redis/internal/executor/transactions/ExecExecutor.java | Java | apache-2.0 | 2,868 |
/**
* Copyright 2010 Google Inc.
*
* 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.waveprotocol.wave.model.waveref;
import junit.framework.TestCase;
import org.waveprotocol.wave.model.id.WaveId;
import org.waveprotocol.wave.model.id.WaveletId;
/**
* Unit tests for {@link WaveRef}
*
* @author meade@google.com <Edwina Mead>
*/
public class WaveRefTest extends TestCase {
public void testBasicEquals() {
WaveRef first = WaveRef.of(WaveId.of("example.com", "w+1234abcd"));
WaveRef second = WaveRef.of(WaveId.of("example.com", "w+1234abcd"));
WaveRef different = WaveRef.of(WaveId.of("test.com", "w+1234abcd"));
assertFalse(first.equals(null));
assertTrue(first.equals(first));
assertTrue(first.equals(second));
assertFalse(first.equals(different));
}
public void testEqualsWithSameWaveIdDifferentOtherFields() {
WaveRef first = WaveRef.of(WaveId.of("example.com", "w+1234abcd"));
WaveRef second = WaveRef.of(WaveId.of("example.com", "w+1234abcd"),
WaveletId.of("example.com", "conv+root"));
WaveRef third = WaveRef.of(WaveId.of("example.com", "w+1234abcd"),
WaveletId.of("example.com", "conv+root"),
"b+12345");
assertTrue(second.equals(second));
assertTrue(third.equals(third));
assertFalse(first.equals(second));
assertFalse(first.equals(third));
assertFalse(second.equals(third));
}
public void testEqualsWithDifferentWaveIdSameOtherFields() {
WaveRef first = WaveRef.of(WaveId.of("test.com", "w+1234"),
WaveletId.of("example.com", "conv+root"),
"b+12345");
WaveRef second = WaveRef.of(WaveId.of("example.com", "w+1234"),
WaveletId.of("example.com", "conv+root"),
"b+12345");
assertFalse(first.equals(second));
}
public void testHashCode() {
WaveRef first = WaveRef.of(WaveId.of("example.com", "w+1234"));
WaveRef second = WaveRef.of(WaveId.of("example.com", "w+1234"),
WaveletId.of("example.com", "conv+root"));
WaveRef third = WaveRef.of(WaveId.of("example.com", "w+1234"),
WaveletId.of("example.com", "conv+root"), "b+12345");
WaveRef sameAsFirst = WaveRef.of(WaveId.of("example.com", "w+1234"));
WaveRef sameAsThird = WaveRef.of(WaveId.of("example.com", "w+1234"),
WaveletId.of("example.com", "conv+root"), "b+12345");
assertEquals(first.hashCode(), sameAsFirst.hashCode());
assertEquals(third.hashCode(), sameAsThird.hashCode());
assertFalse(first.hashCode() == second.hashCode());
assertFalse(first.hashCode() == third.hashCode());
assertFalse(second.hashCode() == third.hashCode());
}
}
| gburd/wave | test/org/waveprotocol/wave/model/waveref/WaveRefTest.java | Java | apache-2.0 | 3,152 |
/**
* @@@ START COPYRIGHT @@@
*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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.
*
* @@@ END COPYRIGHT @@@
**/
package org.trafodion.wms.rest;
import com.sun.jersey.api.core.PackagesResourceConfig;
public class ResourceConfig extends PackagesResourceConfig {
public ResourceConfig() {
super("org.trafodion.wms.rest");
}
}
| apache/incubator-trafodion | wms/src/main/java/org/trafodion/wms/rest/ResourceConfig.java | Java | apache-2.0 | 1,068 |
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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.apache.isis.core.metamodel.services.appfeat;
import java.util.SortedSet;
import com.google.common.base.Function;
import com.google.common.base.Predicate;
import com.google.common.collect.Iterables;
import com.google.common.collect.Sets;
import org.apache.isis.applib.IsisApplibModule;
import org.apache.isis.applib.annotation.Programmatic;
import org.apache.isis.applib.annotation.SemanticsOf;
import org.apache.isis.applib.services.appfeat.ApplicationFeatureRepository;
import org.apache.isis.applib.services.appfeat.ApplicationMemberType;
import org.apache.isis.applib.util.ObjectContracts;
/**
* Canonical application feature, identified by {@link ApplicationFeatureId},
* and wired together with other application features and cached by {@link ApplicationFeatureRepository}.
*
* <p>
* Note that this is NOT a view model; instead it can be converted to a string using methods of
* {@link ApplicationFeatureRepository}, eg {@link ApplicationFeatureRepository#classNamesContainedIn(String, ApplicationMemberType)}.
* </p>
*/
public class ApplicationFeature implements Comparable<ApplicationFeature> {
public static abstract class PropertyDomainEvent<T> extends IsisApplibModule.PropertyDomainEvent<ApplicationFeature, T> {}
public static abstract class CollectionDomainEvent<T> extends IsisApplibModule.CollectionDomainEvent<ApplicationFeature, T> {}
public static abstract class ActionDomainEvent extends IsisApplibModule.ActionDomainEvent<ApplicationFeature> {}
//region > constants
// using same value for all to neaten up rendering
public static final int TYPICAL_LENGTH_PKG_FQN = 50;
public static final int TYPICAL_LENGTH_CLS_NAME = 50;
public static final int TYPICAL_LENGTH_MEMBER_NAME = 50;
//endregion
//region > constructors
public ApplicationFeature() {
this(null);
}
public ApplicationFeature(final ApplicationFeatureId featureId) {
setFeatureId(featureId);
}
//endregion
//region > featureId
private ApplicationFeatureId featureId;
@Programmatic
public ApplicationFeatureId getFeatureId() {
return featureId;
}
public void setFeatureId(final ApplicationFeatureId applicationFeatureId) {
this.featureId = applicationFeatureId;
}
//endregion
//region > memberType
private ApplicationMemberType memberType;
/**
* Only for {@link ApplicationFeatureType#MEMBER member}s.
*/
@Programmatic
public ApplicationMemberType getMemberType() {
return memberType;
}
public void setMemberType(final ApplicationMemberType memberType) {
this.memberType = memberType;
}
//endregion
//region > returnTypeName (for: properties, collections, actions)
private String returnTypeName;
/**
* Only for {@link ApplicationMemberType#ACTION action}s.
*/
@Programmatic
public String getReturnTypeName() {
return returnTypeName;
}
public void setReturnTypeName(final String returnTypeName) {
this.returnTypeName = returnTypeName;
}
//endregion
//region > contributed (for: properties, collections, actions)
private boolean contributed;
@Programmatic
public boolean isContributed() {
return contributed;
}
public void setContributed(final boolean contributed) {
this.contributed = contributed;
}
//endregion
//region > derived (properties and collections)
private Boolean derived;
/**
* Only for {@link ApplicationMemberType#PROPERTY} and {@link ApplicationMemberType#COLLECTION}
*/
@Programmatic
public Boolean isDerived() {
return derived;
}
public void setDerived(final Boolean derived) {
this.derived = derived;
}
//endregion
//region > propertyMaxLength (properties only)
private Integer propertyMaxLength;
/**
* Only for {@link ApplicationMemberType#ACTION action}s.
*/
@Programmatic
public Integer getPropertyMaxLength() {
return propertyMaxLength;
}
public void setPropertyMaxLength(final Integer propertyMaxLength) {
this.propertyMaxLength = propertyMaxLength;
}
//endregion
//region > propertyTypicalLength (properties only)
private Integer propertyTypicalLength;
/**
* Only for {@link ApplicationMemberType#ACTION action}s.
*/
@Programmatic
public Integer getPropertyTypicalLength() {
return propertyTypicalLength;
}
public void setPropertyTypicalLength(final Integer propertyTypicalLength) {
this.propertyTypicalLength = propertyTypicalLength;
}
//endregion
//region > actionSemantics (actions only)
private SemanticsOf actionSemantics;
/**
* Only for {@link ApplicationMemberType#ACTION action}s.
*/
@Programmatic
public SemanticsOf getActionSemantics() {
return actionSemantics;
}
public void setActionSemantics(final SemanticsOf actionSemantics) {
this.actionSemantics = actionSemantics;
}
//endregion
//region > packages: Contents
private final SortedSet<ApplicationFeatureId> contents = Sets.newTreeSet();
@Programmatic
public SortedSet<ApplicationFeatureId> getContents() {
ApplicationFeatureType.ensurePackage(this.getFeatureId());
return contents;
}
@Programmatic
public void addToContents(final ApplicationFeatureId contentId) {
ApplicationFeatureType.ensurePackage(this.getFeatureId());
ApplicationFeatureType.ensurePackageOrClass(contentId);
this.contents.add(contentId);
}
//endregion
//region > classes: Properties, Collections, Actions
private final SortedSet<ApplicationFeatureId> properties = Sets.newTreeSet();
@Programmatic
public SortedSet<ApplicationFeatureId> getProperties() {
ApplicationFeatureType.ensureClass(this.getFeatureId());
return properties;
}
private final SortedSet<ApplicationFeatureId> collections = Sets.newTreeSet();
@Programmatic
public SortedSet<ApplicationFeatureId> getCollections() {
ApplicationFeatureType.ensureClass(this.getFeatureId());
return collections;
}
private final SortedSet<ApplicationFeatureId> actions = Sets.newTreeSet();
@Programmatic
public SortedSet<ApplicationFeatureId> getActions() {
ApplicationFeatureType.ensureClass(this.getFeatureId());
return actions;
}
@Programmatic
public void addToMembers(final ApplicationFeatureId memberId, final ApplicationMemberType memberType) {
ApplicationFeatureType.ensureClass(this.getFeatureId());
ApplicationFeatureType.ensureMember(memberId);
membersOf(memberType).add(memberId);
}
@Programmatic
public SortedSet<ApplicationFeatureId> membersOf(final ApplicationMemberType memberType) {
ApplicationFeatureType.ensureClass(this.getFeatureId());
switch (memberType) {
case PROPERTY:
return properties;
case COLLECTION:
return collections;
default: // case ACTION:
return actions;
}
}
//endregion
//region > Functions
public static class Functions {
private Functions(){}
public static final Function<? super ApplicationFeature, ? extends String> GET_FQN = new Function<ApplicationFeature, String>() {
@Override
public String apply(final ApplicationFeature input) {
return input.getFeatureId().getFullyQualifiedName();
}
};
public static final Function<ApplicationFeature, ApplicationFeatureId> GET_ID =
new Function<ApplicationFeature, ApplicationFeatureId>() {
@Override
public ApplicationFeatureId apply(final ApplicationFeature input) {
return input.getFeatureId();
}
};
}
public static class Predicates {
private Predicates(){}
public static Predicate<ApplicationFeature> packageContainingClasses(
final ApplicationMemberType memberType, final ApplicationFeatureRepositoryDefault applicationFeatures) {
return new Predicate<ApplicationFeature>() {
@Override
public boolean apply(final ApplicationFeature input) {
// all the classes in this package
final Iterable<ApplicationFeatureId> classIds =
Iterables.filter(input.getContents(),
ApplicationFeatureId.Predicates.isClassContaining(memberType, applicationFeatures));
return classIds.iterator().hasNext();
}
};
}
}
//endregion
//region > equals, hashCode, compareTo, toString
private final static String propertyNames = "featureId";
@Override
public int compareTo(final ApplicationFeature other) {
return ObjectContracts.compare(this, other, propertyNames);
}
@Override
public boolean equals(final Object obj) {
return ObjectContracts.equals(this, obj, propertyNames);
}
@Override
public int hashCode() {
return ObjectContracts.hashCode(this, propertyNames);
}
@Override
public String toString() {
return ObjectContracts.toString(this, propertyNames);
}
//endregion
}
| niv0/isis | core/metamodel/src/main/java/org/apache/isis/core/metamodel/services/appfeat/ApplicationFeature.java | Java | apache-2.0 | 10,327 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.