/*
 * 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.execution;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonValue;
import com.google.common.base.Objects;

import java.util.List;

import static com.facebook.presto.execution.QueryId.validateId;
import static com.google.common.base.Preconditions.checkNotNull;

public class TaskId
{
    @JsonCreator
    public static TaskId valueOf(String taskId)
    {
        List<String> ids = QueryId.parseDottedId(taskId, 3, "taskId");
        return new TaskId(new StageId(new QueryId(ids.get(0)), ids.get(1)), ids.get(2));
    }

    private final StageId stageId;
    private final String id;

    public TaskId(String queryId, String stageId, String id)
    {
        this.stageId = new StageId(queryId, stageId);
        this.id = validateId(id);
    }

    public TaskId(StageId stageId, String id)
    {
        this.stageId = checkNotNull(stageId, "stageId is null");
        this.id = validateId(id);
    }

    public QueryId getQueryId()
    {
        return stageId.getQueryId();
    }

    public StageId getStageId()
    {
        return stageId;
    }

    public String getId()
    {
        return id;
    }

    @Override
    @JsonValue
    public String toString()
    {
        return stageId + "." + id;
    }

    @Override
    public int hashCode()
    {
        return Objects.hashCode(stageId, id);
    }

    @Override
    public boolean equals(Object obj)
    {
        if (this == obj) {
            return true;
        }
        if (obj == null || getClass() != obj.getClass()) {
            return false;
        }
        final TaskId other = (TaskId) obj;
        return Objects.equal(this.stageId, other.stageId) &&
                Objects.equal(this.id, other.id);
    }
}

@@ -830,73 +830,8 @@
 eId;
-%0Aimport static com.google.common.base.Preconditions.checkNotNull;
 %0A%0Apu
@@ -933,228 +933,72 @@
     
-List%3CString%3E ids = QueryId.parseDottedId(taskId, 3, %22taskId%22);%0A        return new TaskId(new StageId(new QueryId(ids.get(0)), ids.get(1)), ids.get(2));%0A    %7D%0A%0A    private final StageId stageId;%0A    private final String i
+return new TaskId(taskId);%0A    %7D%0A%0A    private final String fullI
 d;%0A%0A
@@ -1076,86 +1076,80 @@
     
-this.stageId = new StageId(queryId, stageId);%0A        this.id = validateId(
+validateId(id);%0A        this.fullId = queryId + %22.%22 + stageId + %22.%22 + 
 id
-)
 ;%0A  
@@ -1217,225 +1217,426 @@
     
-this.stageId = checkNotNull(sta
+validateId(id);%0A        this.fullId = stageId.getQueryId().
 ge
+t
 Id
-, %22stageId is null%22);%0A        this.id = validateId(id);%0A    %7D%0A%0A    public QueryId getQueryId()%0A    %7B%0A        return stageId.getQueryId();%0A    %7D%0A%0A    public StageId getStageId()%0A    %7B
+() + %22.%22 + stageId.getId() + %22.%22 + id;%0A    %7D%0A%0A    public TaskId(String fullId)%0A    %7B%0A        this.fullId = fullId;%0A    %7D%0A%0A    public QueryId getQueryId()%0A    %7B%0A        return new QueryId(QueryId.parseDottedId(fullId, 3, %22taskId%22).get(0));%0A    %7D%0A%0A    public StageId getStageId()%0A    %7B%0A        List%3CString%3E ids = QueryId.parseDottedId(fullId, 3, %22taskId%22);
 %0A   
@@ -1647,23 +1647,64 @@
  return 
-s
+new S
 tageId
+(new QueryId(ids.get(0)), ids.get(1))
 ;%0A    %7D%0A
@@ -1751,18 +1751,65 @@
  return 
-id
+QueryId.parseDottedId(fullId, 3, %22taskId%22).get(2)
 ;%0A    %7D%0A
@@ -1888,33 +1888,21 @@
  return 
-stageId + %22.%22 + i
+fullI
 d;%0A    %7D
@@ -1985,18 +1985,13 @@
 ode(
-stageId, i
+fullI
 d);%0A
@@ -2289,21 +2289,20 @@
 al(this.
-stage
+full
 Id, othe
@@ -2307,66 +2307,13 @@
 her.
-stageId) &&%0A                Objects.equal(this.id, other.i
+fullI
 d);%0A

Make parsing of taskId parts lazy