This is an automated email from the ASF dual-hosted git repository. ofuks pushed a commit to branch DLAB-1571 in repository https://gitbox.apache.org/repos/asf/incubator-dlab.git
commit 35d053af8de9506cc4e78f6860d6fa70398d535c Author: Oleh Fuks <[email protected]> AuthorDate: Fri Mar 13 13:29:39 2020 +0200 Refactoring --- .../java/com/epam/dlab/mongo/ResourceItem.java | 152 --------------------- .../java/com/epam/dlab/mongo/ResourceItemList.java | 136 ------------------ .../com/epam/dlab/mongo/ResourceItemListTest.java | 54 -------- 3 files changed, 342 deletions(-) diff --git a/services/billing-aws/src/main/java/com/epam/dlab/mongo/ResourceItem.java b/services/billing-aws/src/main/java/com/epam/dlab/mongo/ResourceItem.java deleted file mode 100644 index d92b2d6..0000000 --- a/services/billing-aws/src/main/java/com/epam/dlab/mongo/ResourceItem.java +++ /dev/null @@ -1,152 +0,0 @@ -/* - * 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.epam.dlab.mongo; - -import com.epam.dlab.billing.DlabResourceType; -import com.google.common.base.MoreObjects; -import com.google.common.base.MoreObjects.ToStringHelper; -import org.apache.commons.lang3.StringUtils; - -/** - * The resource of DLab environment. - */ -public class ResourceItem implements Comparable<ResourceItem> { - - private final String project; - /** - * Resource ID. - */ - String resourceId; - - /** - * User friendly name of resource. - */ - String resourceName; - - /** - * Type of resource. - */ - DlabResourceType type; - - /** - * Name of user. - */ - String user; - - /** - * Name of exploratory. - */ - String exploratoryName; - - /** - * Instantiate resource of DLab environment. - * - * @param resourceId resource id. - * @param type the type of resource. - * @param user the name of user. - * @param exploratoryName the name of exploratory. - */ - public ResourceItem(String resourceId, String resourceName, DlabResourceType type, - String user, String exploratoryName, String project) { - this.resourceId = resourceId; - this.resourceName = resourceName; - this.type = type; - this.user = user; - this.exploratoryName = exploratoryName; - this.project = project; - } - - @Override - public int compareTo(ResourceItem o) { - if (o == null) { - return -1; - } - int result = StringUtils.compare(resourceId, o.resourceId); - if (result == 0) { - result = StringUtils.compare(exploratoryName, o.exploratoryName); - if (result == 0) { - result = StringUtils.compare(type.name(), o.type.name()); - if (result == 0) { - return StringUtils.compare(user, o.user); - } - } - } - return result; - } - - - /** - * Returns the resource id. - */ - public String getResourceId() { - return resourceId; - } - - /** - * Return user friendly name of resource. - */ - public String getResourceName() { - return resourceName; - } - - /** - * Returns the type of resource. - */ - public DlabResourceType getType() { - return type; - } - - /** - * Returns the name of user. - */ - public String getUser() { - return user; - } - - /** - * Returns the name of exploratory. - */ - public String getExploratoryName() { - return exploratoryName; - } - - public String getProject() { - return project; - } - - /** - * Returns a string representation of the object. - * - * @param self the object to generate the string for (typically this), used only for its class name. - */ - public ToStringHelper toStringHelper(Object self) { - return MoreObjects.toStringHelper(self) - .add("resourceId", resourceId) - .add("resourceName", resourceName) - .add("type", type) - .add("user", user) - .add("exploratoryName", exploratoryName); - } - - @Override - public String toString() { - return toStringHelper(this).toString(); - } -} diff --git a/services/billing-aws/src/main/java/com/epam/dlab/mongo/ResourceItemList.java b/services/billing-aws/src/main/java/com/epam/dlab/mongo/ResourceItemList.java deleted file mode 100644 index d5c0091..0000000 --- a/services/billing-aws/src/main/java/com/epam/dlab/mongo/ResourceItemList.java +++ /dev/null @@ -1,136 +0,0 @@ -/* - * 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.epam.dlab.mongo; - -import com.epam.dlab.billing.DlabResourceType; -import com.google.common.base.MoreObjects; -import org.apache.commons.lang3.StringUtils; - -import java.util.Collections; -import java.util.Comparator; -import java.util.Vector; - -/** - * List of the DLab's resources. - */ -public class ResourceItemList { - /** - * List of the resources. - */ - private final Vector<ResourceItem> list; - - - /** - * Constructs an empty list of resources. - */ - public ResourceItemList() { - list = new Vector<>(); - } - - - /** - * Appends the resource to the list and returns it. - * - * @param resourceId the resource id. - * @param resourceName the user friendly name of resource. - * @param type the type of resource. - * @param user the name of user. - * @param exploratoryName the name of exploratory. - * @return Instance of the resource. - */ - public ResourceItem append(String resourceId, String resourceName, DlabResourceType type, String user, - String exploratoryName, String project) { - ResourceItem item = new ResourceItem(resourceId, resourceName, type, user, exploratoryName, project); - synchronized (this) { - int index = Collections.binarySearch(list, item); - if (index < 0) { - index = -index; - if (index > list.size()) { - list.add(item); - } else { - list.add(index - 1, item); - } - } else { - item = list.get(index); - } - } - return item; - } - - public ResourceItem append(String resourceId, String resourceName, DlabResourceType type) { - return append(resourceId, resourceName, type, null, null, null); - } - - /** - * Returns the number of the range in list. - */ - public int size() { - return list.size(); - } - - /** - * Returns the resource. - * - * @param index index of the resource. - */ - public ResourceItem get(int index) { - return list.get(index); - } - - /** - * Comparator for search resource item by resource id. - */ - private final ResourceItem findItemById = new ResourceItem(null, null, null, null, null, null); - private final ComparatorByName compareByName = new ComparatorByName(); - - private class ComparatorByName implements Comparator<ResourceItem> { - - @Override - public int compare(ResourceItem o1, ResourceItem o2) { - return StringUtils.compare(o1.resourceId, o2.resourceId); - } - - } - - /** - * Find and return the resource by resource id. - * - * @param resourceId index of the resource. - */ - public ResourceItem getById(String resourceId) { - findItemById.resourceId = resourceId; - int index = Collections.binarySearch(list, findItemById, compareByName); - - return (index < 0 ? null : list.get(index)); - } - - /** - * Removes all of the elements from list. - */ - public void clear() { - list.clear(); - } - - - @Override - public String toString() { - return MoreObjects.toStringHelper(this).add("items", list).toString(); - } -} diff --git a/services/billing-aws/src/test/java/com/epam/dlab/mongo/ResourceItemListTest.java b/services/billing-aws/src/test/java/com/epam/dlab/mongo/ResourceItemListTest.java deleted file mode 100644 index 1c90bb8..0000000 --- a/services/billing-aws/src/test/java/com/epam/dlab/mongo/ResourceItemListTest.java +++ /dev/null @@ -1,54 +0,0 @@ -/* - * 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.epam.dlab.mongo; - -import static junit.framework.TestCase.assertEquals; - -import com.epam.dlab.billing.DlabResourceType; -import org.junit.Test; - -public class ResourceItemListTest { - - @Test - public void append() { - ResourceItemList list = new ResourceItemList(); - list.append("tag-user-nb-exp", "exp", DlabResourceType.EXPLORATORY, "user", "exp", ""); - list.append("tag-user-emr-exp-comp", "comp", DlabResourceType.COMPUTATIONAL, "user", "exp", ""); - - assertEquals(2, list.size()); - - ResourceItem comp = list.get(0); - assertEquals("tag-user-emr-exp-comp", comp.getResourceId()); - assertEquals("comp", comp.getResourceName()); - assertEquals(DlabResourceType.COMPUTATIONAL, comp.getType()); - assertEquals("user", comp.getUser()); - assertEquals("exp", comp.getExploratoryName()); - - ResourceItem exp = list.get(1); - assertEquals("tag-user-nb-exp", exp.getResourceId()); - assertEquals("exp", exp.getResourceName()); - assertEquals(DlabResourceType.EXPLORATORY, exp.getType()); - assertEquals("user", exp.getUser()); - assertEquals("exp", exp.getExploratoryName()); - - list.clear(); - assertEquals(0, list.size()); - } -} --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
